BsManagedComponent.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 "BsScriptAssemblyManager.h"
  11. #include "BsDebug.h"
  12. namespace BansheeEngine
  13. {
  14. ManagedComponent::ManagedComponent()
  15. :mManagedInstance(nullptr), mUpdateThunk(nullptr), mOnDestroyThunk(nullptr), mOnInitializedThunk(nullptr),
  16. mOnResetThunk(nullptr), mMissingType(false), mRequiresReset(true), mOnEnabledThunk(nullptr), mOnDisabledThunk(nullptr)
  17. { }
  18. ManagedComponent::ManagedComponent(const HSceneObject& parent, MonoReflectionType* runtimeType)
  19. : Component(parent), mManagedInstance(nullptr), mRuntimeType(runtimeType), mUpdateThunk(nullptr),
  20. mOnDestroyThunk(nullptr), mOnInitializedThunk(nullptr), mOnResetThunk(nullptr), mMissingType(false),
  21. mRequiresReset(true), mOnEnabledThunk(nullptr), mOnDisabledThunk(nullptr)
  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. setName(mTypeName);
  27. }
  28. ManagedComponent::~ManagedComponent()
  29. {
  30. }
  31. ComponentBackupData ManagedComponent::backup(bool clearExisting)
  32. {
  33. ComponentBackupData backupData;
  34. // If type is not missing read data from actual managed instance, instead just
  35. // return the data we backed up before the type was lost
  36. if (!mMissingType)
  37. {
  38. ManagedSerializableObjectPtr serializableObject = ManagedSerializableObject::createFromExisting(mManagedInstance);
  39. // Serialize the object information and its fields. We cannot just serialize the entire object because
  40. // the managed instance had to be created in a previous step. So we handle creation of the top level object manually.
  41. if (serializableObject != nullptr)
  42. {
  43. MemorySerializer ms;
  44. backupData.size = 0;
  45. backupData.data = ms.encode(serializableObject.get(), backupData.size);
  46. }
  47. else
  48. {
  49. backupData.size = 0;
  50. backupData.data = nullptr;
  51. }
  52. }
  53. else
  54. {
  55. MemorySerializer ms;
  56. backupData.size = 0;
  57. if (mSerializedObjectData != nullptr)
  58. backupData.data = ms.encode(mSerializedObjectData.get(), backupData.size);
  59. else
  60. backupData.data = nullptr;
  61. }
  62. if (clearExisting)
  63. {
  64. if (mManagedInstance != nullptr)
  65. {
  66. mManagedInstance = nullptr;
  67. mono_gchandle_free(mManagedHandle);
  68. mManagedHandle = 0;
  69. }
  70. mRuntimeType = nullptr;
  71. mOnInitializedThunk = nullptr;
  72. mUpdateThunk = nullptr;
  73. mOnDestroyThunk = nullptr;
  74. mOnEnabledThunk = nullptr;
  75. mOnDisabledThunk = nullptr;
  76. }
  77. return backupData;
  78. }
  79. void ManagedComponent::restore(MonoObject* instance, const ComponentBackupData& data, bool missingType)
  80. {
  81. initialize(instance);
  82. mObjInfo = nullptr;
  83. if (instance != nullptr && data.data != nullptr)
  84. {
  85. MemorySerializer ms;
  86. GameObjectManager::instance().startDeserialization();
  87. ManagedSerializableObjectPtr serializableObject = std::static_pointer_cast<ManagedSerializableObject>(ms.decode(data.data, data.size));
  88. GameObjectManager::instance().endDeserialization();
  89. if (!missingType)
  90. {
  91. ScriptAssemblyManager::instance().getSerializableObjectInfo(mNamespace, mTypeName, mObjInfo);
  92. serializableObject->deserialize(instance, mObjInfo);
  93. }
  94. else
  95. mSerializedObjectData = serializableObject;
  96. }
  97. if (!missingType)
  98. mSerializedObjectData = 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. MonoMethod* onDisableMethod = managedClass->getMethod("OnDisable", 0);
  130. if (onDisableMethod != nullptr)
  131. mOnDisabledThunk = (OnDisabledThunkDef)onDisableMethod->getThunk();
  132. MonoMethod* onEnableMethod = managedClass->getMethod("OnEnable", 0);
  133. if (onEnableMethod != nullptr)
  134. mOnEnabledThunk = (OnInitializedThunkDef)onEnableMethod->getThunk();
  135. }
  136. }
  137. bool ManagedComponent::typeEquals(const Component& other)
  138. {
  139. if(Component::typeEquals(other))
  140. {
  141. const ManagedComponent& otherMC = static_cast<const ManagedComponent&>(other);
  142. // Not comparing MonoReflectionType directly because this needs to be able to work before instantiation
  143. return mNamespace == otherMC.getManagedNamespace() && mTypeName == otherMC.getManagedTypeName();
  144. }
  145. return false;
  146. }
  147. void ManagedComponent::update()
  148. {
  149. assert(mManagedInstance != nullptr);
  150. if (mUpdateThunk != 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(mUpdateThunk, mManagedInstance);
  155. }
  156. }
  157. void ManagedComponent::triggerOnReset()
  158. {
  159. assert(mManagedInstance != nullptr);
  160. if (mRequiresReset && mOnResetThunk != nullptr)
  161. {
  162. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  163. // for some extra speed.
  164. MonoUtil::invokeThunk(mOnResetThunk, mManagedInstance);
  165. }
  166. mRequiresReset = false;
  167. }
  168. void ManagedComponent::instantiate()
  169. {
  170. mObjInfo = nullptr;
  171. if (!ScriptAssemblyManager::instance().getSerializableObjectInfo(mNamespace, mTypeName, mObjInfo))
  172. {
  173. MonoObject* instance = ScriptAssemblyManager::instance().getMissingComponentClass()->createInstance(true);
  174. initialize(instance);
  175. mMissingType = true;
  176. }
  177. else
  178. {
  179. initialize(mObjInfo->mMonoClass->createInstance());
  180. mMissingType = false;
  181. }
  182. assert(mManagedInstance != nullptr);
  183. // Find handle to self
  184. HManagedComponent componentHandle;
  185. if (mParent != nullptr)
  186. {
  187. const Vector<HComponent>& components = mParent->getComponents();
  188. for (auto& component : components)
  189. {
  190. if (component.get() == this)
  191. {
  192. componentHandle = component;
  193. break;
  194. }
  195. }
  196. }
  197. assert(componentHandle != nullptr);
  198. ScriptComponent* nativeInstance = ScriptGameObjectManager::instance().createScriptComponent(mManagedInstance, componentHandle);
  199. }
  200. void ManagedComponent::onInitialized()
  201. {
  202. assert(mManagedInstance != nullptr);
  203. if (mSerializedObjectData != nullptr && !mMissingType)
  204. {
  205. mSerializedObjectData->deserialize(mManagedInstance, mObjInfo);
  206. mSerializedObjectData = nullptr;
  207. }
  208. if (mOnInitializedThunk != nullptr)
  209. {
  210. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  211. // for some extra speed.
  212. MonoUtil::invokeThunk(mOnInitializedThunk, mManagedInstance);
  213. }
  214. triggerOnReset();
  215. }
  216. void ManagedComponent::onDestroyed()
  217. {
  218. assert(mManagedInstance != nullptr);
  219. if (mOnDestroyThunk != nullptr)
  220. {
  221. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  222. // for some extra speed.
  223. MonoUtil::invokeThunk(mOnDestroyThunk, mManagedInstance);
  224. }
  225. mManagedInstance = nullptr;
  226. mono_gchandle_free(mManagedHandle);
  227. }
  228. void ManagedComponent::onEnabled()
  229. {
  230. assert(mManagedInstance != nullptr);
  231. if (mOnEnabledThunk != nullptr)
  232. {
  233. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  234. // for some extra speed.
  235. MonoUtil::invokeThunk(mOnEnabledThunk, mManagedInstance);
  236. }
  237. }
  238. void ManagedComponent::onDisabled()
  239. {
  240. assert(mManagedInstance != nullptr);
  241. if (mOnDisabledThunk != nullptr)
  242. {
  243. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  244. // for some extra speed.
  245. MonoUtil::invokeThunk(mOnDisabledThunk, mManagedInstance);
  246. }
  247. }
  248. RTTITypeBase* ManagedComponent::getRTTIStatic()
  249. {
  250. return ManagedComponentRTTI::instance();
  251. }
  252. RTTITypeBase* ManagedComponent::getRTTI() const
  253. {
  254. return ManagedComponent::getRTTIStatic();
  255. }
  256. }