BsManagedComponent.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. void ManagedComponent::update()
  138. {
  139. assert(mManagedInstance != nullptr);
  140. if (mUpdateThunk != nullptr)
  141. {
  142. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  143. // for some extra speed.
  144. MonoUtil::invokeThunk(mUpdateThunk, mManagedInstance);
  145. }
  146. }
  147. void ManagedComponent::triggerOnReset()
  148. {
  149. assert(mManagedInstance != nullptr);
  150. if (mRequiresReset && mOnResetThunk != 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(mOnResetThunk, mManagedInstance);
  155. }
  156. mRequiresReset = false;
  157. }
  158. void ManagedComponent::instantiate()
  159. {
  160. mObjInfo = nullptr;
  161. if (!ScriptAssemblyManager::instance().getSerializableObjectInfo(mNamespace, mTypeName, mObjInfo))
  162. {
  163. MonoObject* instance = ScriptAssemblyManager::instance().getMissingComponentClass()->createInstance(true);
  164. initialize(instance);
  165. mMissingType = true;
  166. }
  167. else
  168. {
  169. initialize(mObjInfo->mMonoClass->createInstance());
  170. mMissingType = false;
  171. }
  172. assert(mManagedInstance != nullptr);
  173. // Find handle to self
  174. HManagedComponent componentHandle;
  175. if (mParent != nullptr)
  176. {
  177. const Vector<HComponent>& components = mParent->getComponents();
  178. for (auto& component : components)
  179. {
  180. if (component.get() == this)
  181. {
  182. componentHandle = component;
  183. break;
  184. }
  185. }
  186. }
  187. assert(componentHandle != nullptr);
  188. ScriptComponent* nativeInstance = ScriptGameObjectManager::instance().createScriptComponent(mManagedInstance, componentHandle);
  189. }
  190. void ManagedComponent::onInitialized()
  191. {
  192. assert(mManagedInstance != nullptr);
  193. if (mSerializedObjectData != nullptr && !mMissingType)
  194. {
  195. mSerializedObjectData->deserialize(mManagedInstance, mObjInfo);
  196. mSerializedObjectData = nullptr;
  197. }
  198. if (mOnInitializedThunk != nullptr)
  199. {
  200. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  201. // for some extra speed.
  202. MonoUtil::invokeThunk(mOnInitializedThunk, mManagedInstance);
  203. }
  204. triggerOnReset();
  205. }
  206. void ManagedComponent::onDestroyed()
  207. {
  208. assert(mManagedInstance != nullptr);
  209. if (mOnDestroyThunk != nullptr)
  210. {
  211. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  212. // for some extra speed.
  213. MonoUtil::invokeThunk(mOnDestroyThunk, mManagedInstance);
  214. }
  215. mManagedInstance = nullptr;
  216. mono_gchandle_free(mManagedHandle);
  217. }
  218. void ManagedComponent::onEnabled()
  219. {
  220. assert(mManagedInstance != nullptr);
  221. if (mOnEnabledThunk != nullptr)
  222. {
  223. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  224. // for some extra speed.
  225. MonoUtil::invokeThunk(mOnEnabledThunk, mManagedInstance);
  226. }
  227. }
  228. void ManagedComponent::onDisabled()
  229. {
  230. assert(mManagedInstance != nullptr);
  231. if (mOnDisabledThunk != 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(mOnDisabledThunk, mManagedInstance);
  236. }
  237. }
  238. RTTITypeBase* ManagedComponent::getRTTIStatic()
  239. {
  240. return ManagedComponentRTTI::instance();
  241. }
  242. RTTITypeBase* ManagedComponent::getRTTI() const
  243. {
  244. return ManagedComponent::getRTTIStatic();
  245. }
  246. }