BsManagedComponent.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsManagedComponent.h"
  4. #include "RTTI/BsManagedComponentRTTI.h"
  5. #include "BsMonoManager.h"
  6. #include "BsMonoClass.h"
  7. #include "BsMonoUtil.h"
  8. #include "BsMonoMethod.h"
  9. #include "Serialization/BsMemorySerializer.h"
  10. #include "Serialization/BsManagedSerializableObject.h"
  11. #include "BsScriptGameObjectManager.h"
  12. #include "Serialization/BsScriptAssemblyManager.h"
  13. #include "Wrappers/BsScriptManagedComponent.h"
  14. #include "BsMonoAssembly.h"
  15. #include "BsPlayInEditorManager.h"
  16. namespace bs
  17. {
  18. ManagedComponent::ManagedComponent()
  19. { }
  20. ManagedComponent::ManagedComponent(const HSceneObject& parent, MonoReflectionType* runtimeType)
  21. : Component(parent), mRuntimeType(runtimeType)
  22. {
  23. MonoUtil::getClassName(mRuntimeType, mNamespace, mTypeName);
  24. setName(mTypeName);
  25. }
  26. ManagedComponent::~ManagedComponent()
  27. {
  28. }
  29. MonoObject* ManagedComponent::getManagedInstance() const
  30. {
  31. if(mGCHandle != 0)
  32. return MonoUtil::getObjectFromGCHandle(mGCHandle);
  33. return nullptr;
  34. }
  35. ComponentBackupData ManagedComponent::backup(bool clearExisting)
  36. {
  37. ComponentBackupData backupData;
  38. // If type is not missing read data from actual managed instance, instead just
  39. // return the data we backed up before the type was lost
  40. if (!mMissingType)
  41. {
  42. MonoObject* instance = MonoUtil::getObjectFromGCHandle(mGCHandle);
  43. SPtr<ManagedSerializableObject> serializableObject = ManagedSerializableObject::createFromExisting(instance);
  44. // Serialize the object information and its fields. We cannot just serialize the entire object because
  45. // the managed instance had to be created in a previous step. So we handle creation of the top level object manually.
  46. if (serializableObject != nullptr)
  47. {
  48. MemorySerializer ms;
  49. backupData.size = 0;
  50. backupData.data = ms.encode(serializableObject.get(), backupData.size);
  51. }
  52. else
  53. {
  54. backupData.size = 0;
  55. backupData.data = nullptr;
  56. }
  57. }
  58. else
  59. {
  60. MemorySerializer ms;
  61. backupData.size = 0;
  62. if (mSerializedObjectData != nullptr)
  63. backupData.data = ms.encode(mSerializedObjectData.get(), backupData.size);
  64. else
  65. backupData.data = nullptr;
  66. }
  67. if (clearExisting)
  68. {
  69. if (mGCHandle != 0)
  70. {
  71. MonoUtil::freeGCHandle(mGCHandle);
  72. mGCHandle = 0;
  73. }
  74. mManagedClass = nullptr;
  75. mRuntimeType = nullptr;
  76. mOnCreatedThunk = nullptr;
  77. mOnInitializedThunk = nullptr;
  78. mOnUpdateThunk = nullptr;
  79. mOnDestroyThunk = nullptr;
  80. mOnEnabledThunk = nullptr;
  81. mOnDisabledThunk = nullptr;
  82. mOnTransformChangedThunk = nullptr;
  83. mCalculateBoundsMethod = nullptr;
  84. }
  85. return backupData;
  86. }
  87. void ManagedComponent::restore(MonoObject* instance, const ComponentBackupData& data, bool missingType)
  88. {
  89. initialize(instance);
  90. mObjInfo = nullptr;
  91. if (instance != nullptr && data.data != nullptr)
  92. {
  93. MemorySerializer ms;
  94. GameObjectManager::instance().startDeserialization();
  95. SPtr<ManagedSerializableObject> serializableObject = std::static_pointer_cast<ManagedSerializableObject>(ms.decode(data.data, data.size));
  96. GameObjectManager::instance().endDeserialization();
  97. if (!missingType)
  98. {
  99. ScriptAssemblyManager::instance().getSerializableObjectInfo(mNamespace, mTypeName, mObjInfo);
  100. serializableObject->deserialize(instance, mObjInfo);
  101. }
  102. else
  103. mSerializedObjectData = serializableObject;
  104. }
  105. if (!missingType)
  106. mSerializedObjectData = nullptr;
  107. mMissingType = missingType;
  108. mRequiresReset = true;
  109. }
  110. void ManagedComponent::initialize(MonoObject* instance)
  111. {
  112. mFullTypeName = mNamespace + "." + mTypeName;
  113. mManagedClass = nullptr;
  114. if (instance != nullptr)
  115. {
  116. mGCHandle = MonoUtil::newGCHandle(instance, false);
  117. ::MonoClass* monoClass = MonoUtil::getClass(instance);
  118. mRuntimeType = MonoUtil::getType(monoClass);
  119. mManagedClass = MonoManager::instance().findClass(monoClass);
  120. }
  121. mOnCreatedThunk = nullptr;
  122. mOnInitializedThunk = nullptr;
  123. mOnUpdateThunk = nullptr;
  124. mOnResetThunk = nullptr;
  125. mOnDestroyThunk = nullptr;
  126. mOnDisabledThunk = nullptr;
  127. mOnEnabledThunk = nullptr;
  128. mOnTransformChangedThunk = nullptr;
  129. mCalculateBoundsMethod = nullptr;
  130. while(mManagedClass != nullptr)
  131. {
  132. if (mOnCreatedThunk == nullptr)
  133. {
  134. MonoMethod* onCreatedMethod = mManagedClass->getMethod("OnCreate", 0);
  135. if (onCreatedMethod != nullptr)
  136. mOnCreatedThunk = (OnInitializedThunkDef)onCreatedMethod->getThunk();
  137. }
  138. if (mOnInitializedThunk == nullptr)
  139. {
  140. MonoMethod* onInitializedMethod = mManagedClass->getMethod("OnInitialize", 0);
  141. if (onInitializedMethod != nullptr)
  142. mOnInitializedThunk = (OnInitializedThunkDef)onInitializedMethod->getThunk();
  143. }
  144. if (mOnUpdateThunk == nullptr)
  145. {
  146. MonoMethod* onUpdateMethod = mManagedClass->getMethod("OnUpdate", 0);
  147. if (onUpdateMethod != nullptr)
  148. mOnUpdateThunk = (OnUpdateThunkDef)onUpdateMethod->getThunk();
  149. }
  150. if (mOnResetThunk == nullptr)
  151. {
  152. MonoMethod* onResetMethod = mManagedClass->getMethod("OnReset", 0);
  153. if (onResetMethod != nullptr)
  154. mOnResetThunk = (OnResetThunkDef)onResetMethod->getThunk();
  155. }
  156. if (mOnDestroyThunk == nullptr)
  157. {
  158. MonoMethod* onDestroyMethod = mManagedClass->getMethod("OnDestroy", 0);
  159. if (onDestroyMethod != nullptr)
  160. mOnDestroyThunk = (OnDestroyedThunkDef)onDestroyMethod->getThunk();
  161. }
  162. if (mOnDisabledThunk == nullptr)
  163. {
  164. MonoMethod* onDisableMethod = mManagedClass->getMethod("OnDisable", 0);
  165. if (onDisableMethod != nullptr)
  166. mOnDisabledThunk = (OnDisabledThunkDef)onDisableMethod->getThunk();
  167. }
  168. if (mOnEnabledThunk == nullptr)
  169. {
  170. MonoMethod* onEnableMethod = mManagedClass->getMethod("OnEnable", 0);
  171. if (onEnableMethod != nullptr)
  172. mOnEnabledThunk = (OnInitializedThunkDef)onEnableMethod->getThunk();
  173. }
  174. if (mOnTransformChangedThunk == nullptr)
  175. {
  176. MonoMethod* onTransformChangedMethod = mManagedClass->getMethod("OnTransformChanged", 1);
  177. if (onTransformChangedMethod != nullptr)
  178. mOnTransformChangedThunk = (OnTransformChangedThunkDef)onTransformChangedMethod->getThunk();
  179. }
  180. if(mCalculateBoundsMethod == nullptr)
  181. mCalculateBoundsMethod = mManagedClass->getMethod("CalculateBounds", 2);
  182. // Search for methods on base class if there is one
  183. MonoClass* baseClass = mManagedClass->getBaseClass();
  184. if (baseClass != ScriptManagedComponent::getMetaData()->scriptClass)
  185. mManagedClass = baseClass;
  186. else
  187. break;
  188. }
  189. if (mManagedClass != nullptr)
  190. {
  191. MonoAssembly* bansheeEngineAssembly = MonoManager::instance().getAssembly(ENGINE_ASSEMBLY);
  192. if (bansheeEngineAssembly == nullptr)
  193. BS_EXCEPT(InvalidStateException, String(ENGINE_ASSEMBLY) + " assembly is not loaded.");
  194. MonoClass* runInEditorAttrib = bansheeEngineAssembly->getClass("BansheeEngine", "RunInEditor");
  195. if (runInEditorAttrib == nullptr)
  196. BS_EXCEPT(InvalidStateException, "Cannot find RunInEditor managed class.");
  197. bool runInEditor = mManagedClass->getAttribute(runInEditorAttrib) != nullptr;
  198. if (runInEditor)
  199. setFlag(ComponentFlag::AlwaysRun, true);
  200. }
  201. }
  202. bool ManagedComponent::typeEquals(const Component& other)
  203. {
  204. if(Component::typeEquals(other))
  205. {
  206. const ManagedComponent& otherMC = static_cast<const ManagedComponent&>(other);
  207. // Not comparing MonoReflectionType directly because this needs to be able to work before instantiation
  208. return mNamespace == otherMC.getManagedNamespace() && mTypeName == otherMC.getManagedTypeName();
  209. }
  210. return false;
  211. }
  212. bool ManagedComponent::calculateBounds(Bounds& bounds)
  213. {
  214. MonoObject* instance;
  215. if(mGCHandle != 0)
  216. instance = MonoUtil::getObjectFromGCHandle(mGCHandle);
  217. else
  218. instance = nullptr;
  219. if (instance != nullptr && mCalculateBoundsMethod != nullptr)
  220. {
  221. AABox box;
  222. Sphere sphere;
  223. void* params[2];
  224. params[0] = &box;
  225. params[1] = &sphere;
  226. MonoObject* areBoundsValidObj = mCalculateBoundsMethod->invokeVirtual(instance, params);
  227. bool areBoundsValid;
  228. areBoundsValid = *(bool*)MonoUtil::unbox(areBoundsValidObj);
  229. bounds = Bounds(box, sphere);
  230. return areBoundsValid;
  231. }
  232. return Component::calculateBounds(bounds);
  233. }
  234. void ManagedComponent::update()
  235. {
  236. if (mOnUpdateThunk != nullptr)
  237. {
  238. MonoObject* instance = MonoUtil::getObjectFromGCHandle(mGCHandle);
  239. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  240. // for some extra speed.
  241. MonoUtil::invokeThunk(mOnUpdateThunk, instance);
  242. }
  243. }
  244. void ManagedComponent::triggerOnReset()
  245. {
  246. if (mRequiresReset && mOnResetThunk != nullptr)
  247. {
  248. MonoObject* instance = MonoUtil::getObjectFromGCHandle(mGCHandle);
  249. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  250. // for some extra speed.
  251. MonoUtil::invokeThunk(mOnResetThunk, instance);
  252. }
  253. mRequiresReset = false;
  254. }
  255. void ManagedComponent::_instantiate()
  256. {
  257. mObjInfo = nullptr;
  258. MonoObject* instance;
  259. if (!ScriptAssemblyManager::instance().getSerializableObjectInfo(mNamespace, mTypeName, mObjInfo))
  260. {
  261. instance = ScriptAssemblyManager::instance().getMissingComponentClass()->createInstance(true);
  262. initialize(instance);
  263. mMissingType = true;
  264. }
  265. else
  266. {
  267. instance = mObjInfo->mMonoClass->createInstance();
  268. initialize(instance);
  269. mMissingType = false;
  270. }
  271. // Find handle to self
  272. HManagedComponent componentHandle;
  273. if (SO() != nullptr)
  274. {
  275. const Vector<HComponent>& components = SO()->getComponents();
  276. for (auto& component : components)
  277. {
  278. if (component.get() == this)
  279. {
  280. componentHandle = component;
  281. break;
  282. }
  283. }
  284. }
  285. assert(componentHandle != nullptr);
  286. ScriptGameObjectManager::instance().createManagedScriptComponent(instance, componentHandle);
  287. }
  288. void ManagedComponent::onCreated()
  289. {
  290. MonoObject* instance = MonoUtil::getObjectFromGCHandle(mGCHandle);
  291. if (mSerializedObjectData != nullptr && !mMissingType)
  292. {
  293. mSerializedObjectData->deserialize(instance, mObjInfo);
  294. mSerializedObjectData = nullptr;
  295. }
  296. if (mOnCreatedThunk != nullptr)
  297. {
  298. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  299. // for some extra speed.
  300. MonoUtil::invokeThunk(mOnCreatedThunk, instance);
  301. }
  302. triggerOnReset();
  303. }
  304. void ManagedComponent::onInitialized()
  305. {
  306. if (mOnInitializedThunk != nullptr)
  307. {
  308. MonoObject* instance = MonoUtil::getObjectFromGCHandle(mGCHandle);
  309. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  310. // for some extra speed.
  311. MonoUtil::invokeThunk(mOnInitializedThunk, instance);
  312. }
  313. triggerOnReset();
  314. }
  315. void ManagedComponent::onDestroyed()
  316. {
  317. if (mOnDestroyThunk != nullptr)
  318. {
  319. MonoObject* instance = MonoUtil::getObjectFromGCHandle(mGCHandle);
  320. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  321. // for some extra speed.
  322. MonoUtil::invokeThunk(mOnDestroyThunk, instance);
  323. }
  324. MonoUtil::freeGCHandle(mGCHandle);
  325. mGCHandle = 0;
  326. }
  327. void ManagedComponent::onEnabled()
  328. {
  329. if (mOnEnabledThunk != nullptr)
  330. {
  331. MonoObject* instance = MonoUtil::getObjectFromGCHandle(mGCHandle);
  332. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  333. // for some extra speed.
  334. MonoUtil::invokeThunk(mOnEnabledThunk, instance);
  335. }
  336. }
  337. void ManagedComponent::onDisabled()
  338. {
  339. if (mOnDisabledThunk != nullptr)
  340. {
  341. MonoObject* instance = MonoUtil::getObjectFromGCHandle(mGCHandle);
  342. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  343. // for some extra speed.
  344. MonoUtil::invokeThunk(mOnDisabledThunk, instance);
  345. }
  346. }
  347. void ManagedComponent::onTransformChanged(TransformChangedFlags flags)
  348. {
  349. if(mOnTransformChangedThunk != nullptr)
  350. {
  351. MonoObject* instance = MonoUtil::getObjectFromGCHandle(mGCHandle);
  352. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  353. // for some extra speed.
  354. MonoUtil::invokeThunk(mOnTransformChangedThunk, instance, flags);
  355. }
  356. }
  357. RTTITypeBase* ManagedComponent::getRTTIStatic()
  358. {
  359. return ManagedComponentRTTI::instance();
  360. }
  361. RTTITypeBase* ManagedComponent::getRTTI() const
  362. {
  363. return ManagedComponent::getRTTIStatic();
  364. }
  365. }