BsManagedComponent.cpp 12 KB

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