BsManagedComponent.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. mCalculateBoundsMethod(nullptr)
  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), mOnEnabledThunk(nullptr), mOnDisabledThunk(nullptr), mCalculateBoundsMethod(nullptr)
  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. setName(mTypeName);
  28. }
  29. ManagedComponent::~ManagedComponent()
  30. {
  31. }
  32. ComponentBackupData ManagedComponent::backup(bool clearExisting)
  33. {
  34. ComponentBackupData backupData;
  35. // If type is not missing read data from actual managed instance, instead just
  36. // return the data we backed up before the type was lost
  37. if (!mMissingType)
  38. {
  39. ManagedSerializableObjectPtr serializableObject = ManagedSerializableObject::createFromExisting(mManagedInstance);
  40. // Serialize the object information and its fields. We cannot just serialize the entire object because
  41. // the managed instance had to be created in a previous step. So we handle creation of the top level object manually.
  42. if (serializableObject != nullptr)
  43. {
  44. MemorySerializer ms;
  45. backupData.size = 0;
  46. backupData.data = ms.encode(serializableObject.get(), backupData.size);
  47. }
  48. else
  49. {
  50. backupData.size = 0;
  51. backupData.data = nullptr;
  52. }
  53. }
  54. else
  55. {
  56. MemorySerializer ms;
  57. backupData.size = 0;
  58. if (mSerializedObjectData != nullptr)
  59. backupData.data = ms.encode(mSerializedObjectData.get(), backupData.size);
  60. else
  61. backupData.data = nullptr;
  62. }
  63. if (clearExisting)
  64. {
  65. if (mManagedInstance != nullptr)
  66. {
  67. mManagedInstance = nullptr;
  68. mono_gchandle_free(mManagedHandle);
  69. mManagedHandle = 0;
  70. }
  71. mRuntimeType = nullptr;
  72. mOnInitializedThunk = nullptr;
  73. mUpdateThunk = nullptr;
  74. mOnDestroyThunk = nullptr;
  75. mOnEnabledThunk = nullptr;
  76. mOnDisabledThunk = nullptr;
  77. mCalculateBoundsMethod = nullptr;
  78. }
  79. return backupData;
  80. }
  81. void ManagedComponent::restore(MonoObject* instance, const ComponentBackupData& data, bool missingType)
  82. {
  83. initialize(instance);
  84. mObjInfo = nullptr;
  85. if (instance != nullptr && data.data != nullptr)
  86. {
  87. MemorySerializer ms;
  88. GameObjectManager::instance().startDeserialization();
  89. ManagedSerializableObjectPtr serializableObject = std::static_pointer_cast<ManagedSerializableObject>(ms.decode(data.data, data.size));
  90. GameObjectManager::instance().endDeserialization();
  91. if (!missingType)
  92. {
  93. ScriptAssemblyManager::instance().getSerializableObjectInfo(mNamespace, mTypeName, mObjInfo);
  94. serializableObject->deserialize(instance, mObjInfo);
  95. }
  96. else
  97. mSerializedObjectData = serializableObject;
  98. }
  99. if (!missingType)
  100. mSerializedObjectData = nullptr;
  101. mMissingType = missingType;
  102. mRequiresReset = true;
  103. }
  104. void ManagedComponent::initialize(MonoObject* object)
  105. {
  106. mFullTypeName = mNamespace + "." + mTypeName;
  107. mManagedInstance = object;
  108. MonoClass* managedClass = nullptr;
  109. if (mManagedInstance != nullptr)
  110. {
  111. mManagedHandle = mono_gchandle_new(mManagedInstance, false);
  112. ::MonoClass* monoClass = mono_object_get_class(object);
  113. MonoType* monoType = mono_class_get_type(monoClass);
  114. mRuntimeType = mono_type_get_object(MonoManager::instance().getDomain(), monoType);
  115. managedClass = MonoManager::instance().findClass(monoClass);
  116. }
  117. if (managedClass != nullptr)
  118. {
  119. MonoMethod* onInitializedMethod = managedClass->getMethod("OnInitialize", 0);
  120. if (onInitializedMethod != nullptr)
  121. mOnInitializedThunk = (OnInitializedThunkDef)onInitializedMethod->getThunk();
  122. MonoMethod* updateMethod = managedClass->getMethod("Update", 0);
  123. if (updateMethod != nullptr)
  124. mUpdateThunk = (UpdateThunkDef)updateMethod->getThunk();
  125. MonoMethod* onResetMethod = managedClass->getMethod("OnReset", 0);
  126. if (onResetMethod != nullptr)
  127. mOnResetThunk = (OnResetThunkDef)onResetMethod->getThunk();
  128. MonoMethod* onDestroyMethod = managedClass->getMethod("OnDestroy", 0);
  129. if (onDestroyMethod != nullptr)
  130. mOnDestroyThunk = (OnDestroyedThunkDef)onDestroyMethod->getThunk();
  131. MonoMethod* onDisableMethod = managedClass->getMethod("OnDisable", 0);
  132. if (onDisableMethod != nullptr)
  133. mOnDisabledThunk = (OnDisabledThunkDef)onDisableMethod->getThunk();
  134. MonoMethod* onEnableMethod = managedClass->getMethod("OnEnable", 0);
  135. if (onEnableMethod != nullptr)
  136. mOnEnabledThunk = (OnInitializedThunkDef)onEnableMethod->getThunk();
  137. mCalculateBoundsMethod = managedClass->getMethod("CalculateBounds", 2);
  138. }
  139. }
  140. bool ManagedComponent::typeEquals(const Component& other)
  141. {
  142. if(Component::typeEquals(other))
  143. {
  144. const ManagedComponent& otherMC = static_cast<const ManagedComponent&>(other);
  145. // Not comparing MonoReflectionType directly because this needs to be able to work before instantiation
  146. return mNamespace == otherMC.getManagedNamespace() && mTypeName == otherMC.getManagedTypeName();
  147. }
  148. return false;
  149. }
  150. bool ManagedComponent::calculateBounds(Bounds& bounds)
  151. {
  152. if (mManagedInstance != nullptr && mCalculateBoundsMethod != nullptr)
  153. {
  154. AABox box;
  155. Sphere sphere;
  156. void* params[2];
  157. params[0] = &box;
  158. params[1] = &sphere;
  159. MonoObject* areBoundsValidObj = mCalculateBoundsMethod->invokeVirtual(mManagedInstance, params);
  160. bool areBoundsValid;
  161. areBoundsValid = *(bool*)mono_object_unbox(areBoundsValidObj);
  162. bounds = Bounds(box, sphere);
  163. return areBoundsValid;
  164. }
  165. return Component::calculateBounds(bounds);
  166. }
  167. void ManagedComponent::update()
  168. {
  169. assert(mManagedInstance != nullptr);
  170. if (mUpdateThunk != nullptr)
  171. {
  172. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  173. // for some extra speed.
  174. MonoUtil::invokeThunk(mUpdateThunk, mManagedInstance);
  175. }
  176. }
  177. void ManagedComponent::triggerOnReset()
  178. {
  179. assert(mManagedInstance != nullptr);
  180. if (mRequiresReset && mOnResetThunk != 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(mOnResetThunk, mManagedInstance);
  185. }
  186. mRequiresReset = false;
  187. }
  188. void ManagedComponent::instantiate()
  189. {
  190. mObjInfo = nullptr;
  191. if (!ScriptAssemblyManager::instance().getSerializableObjectInfo(mNamespace, mTypeName, mObjInfo))
  192. {
  193. MonoObject* instance = ScriptAssemblyManager::instance().getMissingComponentClass()->createInstance(true);
  194. initialize(instance);
  195. mMissingType = true;
  196. }
  197. else
  198. {
  199. initialize(mObjInfo->mMonoClass->createInstance());
  200. mMissingType = false;
  201. }
  202. assert(mManagedInstance != nullptr);
  203. // Find handle to self
  204. HManagedComponent componentHandle;
  205. if (mParent != nullptr)
  206. {
  207. const Vector<HComponent>& components = mParent->getComponents();
  208. for (auto& component : components)
  209. {
  210. if (component.get() == this)
  211. {
  212. componentHandle = component;
  213. break;
  214. }
  215. }
  216. }
  217. assert(componentHandle != nullptr);
  218. ScriptComponent* nativeInstance = ScriptGameObjectManager::instance().createScriptComponent(mManagedInstance, componentHandle);
  219. }
  220. void ManagedComponent::onInitialized()
  221. {
  222. assert(mManagedInstance != nullptr);
  223. if (mSerializedObjectData != nullptr && !mMissingType)
  224. {
  225. mSerializedObjectData->deserialize(mManagedInstance, mObjInfo);
  226. mSerializedObjectData = nullptr;
  227. }
  228. if (mOnInitializedThunk != nullptr)
  229. {
  230. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  231. // for some extra speed.
  232. MonoUtil::invokeThunk(mOnInitializedThunk, mManagedInstance);
  233. }
  234. triggerOnReset();
  235. }
  236. void ManagedComponent::onDestroyed()
  237. {
  238. assert(mManagedInstance != nullptr);
  239. if (mOnDestroyThunk != nullptr)
  240. {
  241. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  242. // for some extra speed.
  243. MonoUtil::invokeThunk(mOnDestroyThunk, mManagedInstance);
  244. }
  245. mManagedInstance = nullptr;
  246. mono_gchandle_free(mManagedHandle);
  247. }
  248. void ManagedComponent::onEnabled()
  249. {
  250. assert(mManagedInstance != nullptr);
  251. if (mOnEnabledThunk != nullptr)
  252. {
  253. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  254. // for some extra speed.
  255. MonoUtil::invokeThunk(mOnEnabledThunk, mManagedInstance);
  256. }
  257. }
  258. void ManagedComponent::onDisabled()
  259. {
  260. assert(mManagedInstance != nullptr);
  261. if (mOnDisabledThunk != nullptr)
  262. {
  263. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  264. // for some extra speed.
  265. MonoUtil::invokeThunk(mOnDisabledThunk, mManagedInstance);
  266. }
  267. }
  268. RTTITypeBase* ManagedComponent::getRTTIStatic()
  269. {
  270. return ManagedComponentRTTI::instance();
  271. }
  272. RTTITypeBase* ManagedComponent::getRTTI() const
  273. {
  274. return ManagedComponent::getRTTIStatic();
  275. }
  276. }