BsManagedComponent.cpp 12 KB

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