BsManagedComponent.cpp 13 KB

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