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. #include "Utility/BsUtility.h"
  17. namespace bs
  18. {
  19. ManagedComponent::ManagedComponent()
  20. { }
  21. ManagedComponent::ManagedComponent(const HSceneObject& parent, MonoReflectionType* runtimeType)
  22. : Component(parent), mRuntimeType(runtimeType)
  23. {
  24. MonoUtil::getClassName(mRuntimeType, mNamespace, mTypeName);
  25. setName(mTypeName);
  26. }
  27. ManagedComponent::~ManagedComponent()
  28. {
  29. }
  30. MonoObject* ManagedComponent::getManagedInstance() const
  31. {
  32. if(mOwner)
  33. return mOwner->getManagedInstance();
  34. return nullptr;
  35. }
  36. RawBackupData ManagedComponent::backup(bool clearExisting)
  37. {
  38. RawBackupData backupData;
  39. // If type is not missing read data from actual managed instance, instead just
  40. // return the data we backed up before the type was lost
  41. if (!mMissingType)
  42. {
  43. MonoObject* instance = mOwner->getManagedInstance();
  44. SPtr<ManagedSerializableObject> serializableObject = ManagedSerializableObject::createFromExisting(instance);
  45. // Serialize the object information and its fields. We cannot just serialize the entire object because
  46. // the managed instance had to be created in a previous step. So we handle creation of the top level object manually.
  47. if (serializableObject != nullptr)
  48. {
  49. MemorySerializer ms;
  50. backupData.size = 0;
  51. backupData.data = ms.encode(serializableObject.get(), backupData.size);
  52. }
  53. else
  54. {
  55. backupData.size = 0;
  56. backupData.data = nullptr;
  57. }
  58. }
  59. else
  60. {
  61. MemorySerializer ms;
  62. backupData.size = 0;
  63. if (mSerializedObjectData != nullptr)
  64. backupData.data = ms.encode(mSerializedObjectData.get(), backupData.size);
  65. else
  66. backupData.data = nullptr;
  67. }
  68. if (clearExisting)
  69. {
  70. mManagedClass = nullptr;
  71. mRuntimeType = nullptr;
  72. mOnCreatedThunk = nullptr;
  73. mOnInitializedThunk = nullptr;
  74. mOnUpdateThunk = nullptr;
  75. mOnDestroyThunk = nullptr;
  76. mOnEnabledThunk = nullptr;
  77. mOnDisabledThunk = nullptr;
  78. mOnTransformChangedThunk = nullptr;
  79. mCalculateBoundsMethod = nullptr;
  80. }
  81. return backupData;
  82. }
  83. void ManagedComponent::restore(const RawBackupData& data, bool missingType)
  84. {
  85. initialize(mOwner);
  86. mObjInfo = nullptr;
  87. MonoObject* instance = mOwner->getManagedInstance();
  88. if (instance != nullptr && data.data != nullptr)
  89. {
  90. MemorySerializer ms;
  91. CoreSerializationContext serzContext;
  92. serzContext.goState = bs_shared_ptr_new<GameObjectDeserializationState>();
  93. auto serializableObject = std::static_pointer_cast<ManagedSerializableObject>(
  94. ms.decode(data.data, data.size, &serzContext));
  95. serzContext.goState->resolve();
  96. if (!missingType)
  97. {
  98. ScriptAssemblyManager::instance().getSerializableObjectInfo(mNamespace, mTypeName, mObjInfo);
  99. serializableObject->deserialize(instance, mObjInfo);
  100. }
  101. else
  102. mSerializedObjectData = serializableObject;
  103. }
  104. if (!missingType)
  105. mSerializedObjectData = nullptr;
  106. mMissingType = missingType;
  107. mRequiresReset = true;
  108. }
  109. void ManagedComponent::initialize(ScriptManagedComponent* owner)
  110. {
  111. mOwner = owner;
  112. mFullTypeName = mNamespace + "." + mTypeName;
  113. MonoObject* instance = owner->getManagedInstance();
  114. mManagedClass = nullptr;
  115. if (instance != nullptr)
  116. {
  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 = nullptr;
  215. if(mOwner)
  216. instance = mOwner->getManagedInstance();
  217. if (instance != nullptr && mCalculateBoundsMethod != nullptr)
  218. {
  219. AABox box;
  220. Sphere sphere;
  221. void* params[2];
  222. params[0] = &box;
  223. params[1] = &sphere;
  224. MonoObject* areBoundsValidObj = mCalculateBoundsMethod->invokeVirtual(instance, params);
  225. bool areBoundsValid;
  226. areBoundsValid = *(bool*)MonoUtil::unbox(areBoundsValidObj);
  227. bounds = Bounds(box, sphere);
  228. return areBoundsValid;
  229. }
  230. return Component::calculateBounds(bounds);
  231. }
  232. void ManagedComponent::update()
  233. {
  234. if (mOnUpdateThunk != nullptr)
  235. {
  236. MonoObject* instance = mOwner->getManagedInstance();
  237. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  238. // for some extra speed.
  239. MonoUtil::invokeThunk(mOnUpdateThunk, instance);
  240. }
  241. }
  242. void ManagedComponent::triggerOnReset()
  243. {
  244. if (mRequiresReset && mOnResetThunk != nullptr)
  245. {
  246. MonoObject* instance = mOwner->getManagedInstance();
  247. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  248. // for some extra speed.
  249. MonoUtil::invokeThunk(mOnResetThunk, instance);
  250. }
  251. mRequiresReset = false;
  252. }
  253. void ManagedComponent::_instantiate()
  254. {
  255. mObjInfo = nullptr;
  256. MonoObject* instance;
  257. if (!ScriptAssemblyManager::instance().getSerializableObjectInfo(mNamespace, mTypeName, mObjInfo))
  258. {
  259. instance = ScriptAssemblyManager::instance().getBuiltinClasses().missingComponentClass->createInstance(true);
  260. mMissingType = true;
  261. }
  262. else
  263. {
  264. instance = mObjInfo->mMonoClass->createInstance();
  265. mMissingType = false;
  266. }
  267. // Find handle to self
  268. HManagedComponent componentHandle;
  269. if (SO() != nullptr)
  270. {
  271. const Vector<HComponent>& components = SO()->getComponents();
  272. for (auto& component : components)
  273. {
  274. if (component.get() == this)
  275. {
  276. componentHandle = static_object_cast<ManagedComponent>(component);
  277. break;
  278. }
  279. }
  280. }
  281. assert(componentHandle != nullptr);
  282. ScriptGameObjectManager::instance().createManagedScriptComponent(instance, componentHandle);
  283. }
  284. void ManagedComponent::onCreated()
  285. {
  286. MonoObject* instance = mOwner->getManagedInstance();
  287. if (mSerializedObjectData != nullptr && !mMissingType)
  288. {
  289. mSerializedObjectData->deserialize(instance, mObjInfo);
  290. mSerializedObjectData = nullptr;
  291. }
  292. if (mOnCreatedThunk != nullptr)
  293. {
  294. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  295. // for some extra speed.
  296. MonoUtil::invokeThunk(mOnCreatedThunk, instance);
  297. }
  298. triggerOnReset();
  299. }
  300. void ManagedComponent::onInitialized()
  301. {
  302. if (mOnInitializedThunk != nullptr)
  303. {
  304. MonoObject* instance = mOwner->getManagedInstance();
  305. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  306. // for some extra speed.
  307. MonoUtil::invokeThunk(mOnInitializedThunk, instance);
  308. }
  309. triggerOnReset();
  310. }
  311. void ManagedComponent::onDestroyed()
  312. {
  313. if (mOnDestroyThunk != nullptr)
  314. {
  315. MonoObject* instance = mOwner->getManagedInstance();
  316. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  317. // for some extra speed.
  318. MonoUtil::invokeThunk(mOnDestroyThunk, instance);
  319. }
  320. }
  321. void ManagedComponent::onEnabled()
  322. {
  323. if (mOnEnabledThunk != nullptr)
  324. {
  325. MonoObject* instance = mOwner->getManagedInstance();
  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 = mOwner->getManagedInstance();
  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 = mOwner->getManagedInstance();
  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. }