BsManagedComponent.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsManagedComponent.h"
  4. #include "BsManagedComponentRTTI.h"
  5. #include "BsMonoManager.h"
  6. #include "BsMonoClass.h"
  7. #include "BsMonoUtil.h"
  8. #include "BsMonoMethod.h"
  9. #include "BsMemorySerializer.h"
  10. #include "BsManagedSerializableObject.h"
  11. #include "BsScriptGameObjectManager.h"
  12. #include "BsScriptAssemblyManager.h"
  13. #include "BsScriptManagedComponent.h"
  14. #include "BsMonoAssembly.h"
  15. #include "BsPlayInEditorManager.h"
  16. namespace bs
  17. {
  18. ManagedComponent::ManagedComponent()
  19. : mManagedInstance(nullptr), mManagedClass(nullptr), mRuntimeType(nullptr), mManagedHandle(0), mRunInEditor(false)
  20. , mRequiresReset(true), mMissingType(false), mOnInitializedThunk(nullptr), mOnUpdateThunk(nullptr)
  21. , mOnResetThunk(nullptr), mOnDestroyThunk(nullptr), mOnDisabledThunk(nullptr), mOnEnabledThunk(nullptr)
  22. , mOnTransformChangedThunk(nullptr), mCalculateBoundsMethod(nullptr)
  23. { }
  24. ManagedComponent::ManagedComponent(const HSceneObject& parent, MonoReflectionType* runtimeType)
  25. : Component(parent), mManagedInstance(nullptr), mManagedClass(nullptr), mRuntimeType(runtimeType)
  26. , mManagedHandle(0), mRunInEditor(false), mRequiresReset(true), mMissingType(false), mOnInitializedThunk(nullptr)
  27. , mOnUpdateThunk(nullptr), mOnResetThunk(nullptr), mOnDestroyThunk(nullptr), mOnDisabledThunk(nullptr)
  28. , mOnEnabledThunk(nullptr), mOnTransformChangedThunk(nullptr), mCalculateBoundsMethod(nullptr)
  29. {
  30. MonoUtil::getClassName(mRuntimeType, mNamespace, mTypeName);
  31. setName(mTypeName);
  32. }
  33. ManagedComponent::~ManagedComponent()
  34. {
  35. }
  36. ComponentBackupData ManagedComponent::backup(bool clearExisting)
  37. {
  38. ComponentBackupData 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. SPtr<ManagedSerializableObject> serializableObject = ManagedSerializableObject::createFromExisting(mManagedInstance);
  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 (mManagedInstance != nullptr)
  70. {
  71. mManagedInstance = nullptr;
  72. MonoUtil::freeGCHandle(mManagedHandle);
  73. mManagedHandle = 0;
  74. }
  75. mManagedClass = nullptr;
  76. mRuntimeType = 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* object)
  111. {
  112. mFullTypeName = mNamespace + "." + mTypeName;
  113. mManagedInstance = object;
  114. mManagedClass = nullptr;
  115. if (mManagedInstance != nullptr)
  116. {
  117. mManagedHandle = MonoUtil::newGCHandle(mManagedInstance);
  118. ::MonoClass* monoClass = MonoUtil::getClass(object);
  119. mRuntimeType = MonoUtil::getType(monoClass);
  120. mManagedClass = MonoManager::instance().findClass(monoClass);
  121. }
  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 (mOnInitializedThunk == nullptr)
  133. {
  134. MonoMethod* onInitializedMethod = mManagedClass->getMethod("OnInitialize", 0);
  135. if (onInitializedMethod != nullptr)
  136. mOnInitializedThunk = (OnInitializedThunkDef)onInitializedMethod->getThunk();
  137. }
  138. if (mOnUpdateThunk == nullptr)
  139. {
  140. MonoMethod* onUpdateMethod = mManagedClass->getMethod("OnUpdate", 0);
  141. if (onUpdateMethod != nullptr)
  142. mOnUpdateThunk = (OnUpdateThunkDef)onUpdateMethod->getThunk();
  143. }
  144. if (mOnResetThunk == nullptr)
  145. {
  146. MonoMethod* onResetMethod = mManagedClass->getMethod("OnReset", 0);
  147. if (onResetMethod != nullptr)
  148. mOnResetThunk = (OnResetThunkDef)onResetMethod->getThunk();
  149. }
  150. if (mOnDestroyThunk == nullptr)
  151. {
  152. MonoMethod* onDestroyMethod = mManagedClass->getMethod("OnDestroy", 0);
  153. if (onDestroyMethod != nullptr)
  154. mOnDestroyThunk = (OnDestroyedThunkDef)onDestroyMethod->getThunk();
  155. }
  156. if (mOnDisabledThunk == nullptr)
  157. {
  158. MonoMethod* onDisableMethod = mManagedClass->getMethod("OnDisable", 0);
  159. if (onDisableMethod != nullptr)
  160. mOnDisabledThunk = (OnDisabledThunkDef)onDisableMethod->getThunk();
  161. }
  162. if (mOnEnabledThunk == nullptr)
  163. {
  164. MonoMethod* onEnableMethod = mManagedClass->getMethod("OnEnable", 0);
  165. if (onEnableMethod != nullptr)
  166. mOnEnabledThunk = (OnInitializedThunkDef)onEnableMethod->getThunk();
  167. }
  168. if (mOnTransformChangedThunk == nullptr)
  169. {
  170. MonoMethod* onTransformChangedMethod = mManagedClass->getMethod("OnTransformChanged", 1);
  171. if (onTransformChangedMethod != nullptr)
  172. mOnTransformChangedThunk = (OnTransformChangedThunkDef)onTransformChangedMethod->getThunk();
  173. }
  174. if(mCalculateBoundsMethod == nullptr)
  175. mCalculateBoundsMethod = mManagedClass->getMethod("CalculateBounds", 2);
  176. // Search for methods on base class if there is one
  177. MonoClass* baseClass = mManagedClass->getBaseClass();
  178. if (baseClass != ScriptManagedComponent::getMetaData()->scriptClass)
  179. mManagedClass = baseClass;
  180. else
  181. break;
  182. }
  183. if (mManagedClass != nullptr)
  184. {
  185. MonoAssembly* bansheeEngineAssembly = MonoManager::instance().getAssembly(ENGINE_ASSEMBLY);
  186. if (bansheeEngineAssembly == nullptr)
  187. BS_EXCEPT(InvalidStateException, String(ENGINE_ASSEMBLY) + " assembly is not loaded.");
  188. MonoClass* runInEditorAttrib = bansheeEngineAssembly->getClass("BansheeEngine", "RunInEditor");
  189. if (runInEditorAttrib == nullptr)
  190. BS_EXCEPT(InvalidStateException, "Cannot find RunInEditor managed class.");
  191. mRunInEditor = mManagedClass->getAttribute(runInEditorAttrib) != nullptr;
  192. }
  193. else
  194. mRunInEditor = false;
  195. }
  196. bool ManagedComponent::typeEquals(const Component& other)
  197. {
  198. if(Component::typeEquals(other))
  199. {
  200. const ManagedComponent& otherMC = static_cast<const ManagedComponent&>(other);
  201. // Not comparing MonoReflectionType directly because this needs to be able to work before instantiation
  202. return mNamespace == otherMC.getManagedNamespace() && mTypeName == otherMC.getManagedTypeName();
  203. }
  204. return false;
  205. }
  206. bool ManagedComponent::calculateBounds(Bounds& bounds)
  207. {
  208. if (mManagedInstance != nullptr && mCalculateBoundsMethod != nullptr)
  209. {
  210. AABox box;
  211. Sphere sphere;
  212. void* params[2];
  213. params[0] = &box;
  214. params[1] = &sphere;
  215. MonoObject* areBoundsValidObj = mCalculateBoundsMethod->invokeVirtual(mManagedInstance, params);
  216. bool areBoundsValid;
  217. areBoundsValid = *(bool*)MonoUtil::unbox(areBoundsValidObj);
  218. bounds = Bounds(box, sphere);
  219. return areBoundsValid;
  220. }
  221. return Component::calculateBounds(bounds);
  222. }
  223. void ManagedComponent::update()
  224. {
  225. if (PlayInEditorManager::instance().getState() != PlayInEditorState::Playing && !mRunInEditor)
  226. return;
  227. assert(mManagedInstance != nullptr);
  228. if (mOnUpdateThunk != 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(mOnUpdateThunk, mManagedInstance);
  233. }
  234. }
  235. void ManagedComponent::triggerOnInitialize()
  236. {
  237. if (PlayInEditorManager::instance().getState() == PlayInEditorState::Stopped && !mRunInEditor)
  238. return;
  239. if (mOnInitializedThunk != 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(mOnInitializedThunk, mManagedInstance);
  244. }
  245. }
  246. void ManagedComponent::triggerOnReset()
  247. {
  248. assert(mManagedInstance != nullptr);
  249. if (mRequiresReset && mOnResetThunk != nullptr)
  250. {
  251. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  252. // for some extra speed.
  253. MonoUtil::invokeThunk(mOnResetThunk, mManagedInstance);
  254. }
  255. mRequiresReset = false;
  256. }
  257. void ManagedComponent::triggerOnEnable()
  258. {
  259. if (PlayInEditorManager::instance().getState() == PlayInEditorState::Stopped && !mRunInEditor)
  260. return;
  261. assert(mManagedInstance != nullptr);
  262. if (mOnEnabledThunk != nullptr)
  263. {
  264. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  265. // for some extra speed.
  266. MonoUtil::invokeThunk(mOnEnabledThunk, mManagedInstance);
  267. }
  268. }
  269. void ManagedComponent::_instantiate()
  270. {
  271. mObjInfo = nullptr;
  272. if (!ScriptAssemblyManager::instance().getSerializableObjectInfo(mNamespace, mTypeName, mObjInfo))
  273. {
  274. MonoObject* instance = ScriptAssemblyManager::instance().getMissingComponentClass()->createInstance(true);
  275. initialize(instance);
  276. mMissingType = true;
  277. }
  278. else
  279. {
  280. initialize(mObjInfo->mMonoClass->createInstance());
  281. mMissingType = false;
  282. }
  283. assert(mManagedInstance != nullptr);
  284. // Find handle to self
  285. HManagedComponent componentHandle;
  286. if (SO() != nullptr)
  287. {
  288. const Vector<HComponent>& components = SO()->getComponents();
  289. for (auto& component : components)
  290. {
  291. if (component.get() == this)
  292. {
  293. componentHandle = component;
  294. break;
  295. }
  296. }
  297. }
  298. assert(componentHandle != nullptr);
  299. ScriptGameObjectManager::instance().createManagedScriptComponent(mManagedInstance, componentHandle);
  300. }
  301. void ManagedComponent::onInitialized()
  302. {
  303. assert(mManagedInstance != nullptr);
  304. if (mSerializedObjectData != nullptr && !mMissingType)
  305. {
  306. mSerializedObjectData->deserialize(mManagedInstance, mObjInfo);
  307. mSerializedObjectData = nullptr;
  308. }
  309. triggerOnInitialize();
  310. triggerOnReset();
  311. }
  312. void ManagedComponent::onDestroyed()
  313. {
  314. assert(mManagedInstance != nullptr);
  315. if (mOnDestroyThunk != nullptr)
  316. {
  317. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  318. // for some extra speed.
  319. MonoUtil::invokeThunk(mOnDestroyThunk, mManagedInstance);
  320. }
  321. mManagedInstance = nullptr;
  322. MonoUtil::freeGCHandle(mManagedHandle);
  323. }
  324. void ManagedComponent::onEnabled()
  325. {
  326. triggerOnEnable();
  327. }
  328. void ManagedComponent::onDisabled()
  329. {
  330. if (PlayInEditorManager::instance().getState() == PlayInEditorState::Stopped && !mRunInEditor)
  331. return;
  332. assert(mManagedInstance != nullptr);
  333. if (mOnDisabledThunk != nullptr)
  334. {
  335. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  336. // for some extra speed.
  337. MonoUtil::invokeThunk(mOnDisabledThunk, mManagedInstance);
  338. }
  339. }
  340. void ManagedComponent::onTransformChanged(TransformChangedFlags flags)
  341. {
  342. if (PlayInEditorManager::instance().getState() == PlayInEditorState::Stopped && !mRunInEditor)
  343. return;
  344. if(mOnTransformChangedThunk != nullptr)
  345. {
  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, mManagedInstance, 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. }