BsManagedComponent.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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 BansheeEngine
  16. {
  17. ManagedComponent::ManagedComponent()
  18. : mManagedInstance(nullptr), mRuntimeType(nullptr), mManagedHandle(0), mRunInEditor(false), mRequiresReset(true)
  19. , mMissingType(false), mOnInitializedThunk(nullptr), mOnUpdateThunk(nullptr), mOnResetThunk(nullptr)
  20. , mOnDestroyThunk(nullptr), mOnDisabledThunk(nullptr), mOnEnabledThunk(nullptr), mOnTransformChangedThunk(nullptr)
  21. , mCalculateBoundsMethod(nullptr)
  22. { }
  23. ManagedComponent::ManagedComponent(const HSceneObject& parent, MonoReflectionType* runtimeType)
  24. : Component(parent), mManagedInstance(nullptr), mRuntimeType(runtimeType), mManagedHandle(0), mRunInEditor(false)
  25. , mRequiresReset(true), mMissingType(false), mOnInitializedThunk(nullptr), mOnUpdateThunk(nullptr)
  26. , mOnResetThunk(nullptr), mOnDestroyThunk(nullptr), mOnDisabledThunk(nullptr), mOnEnabledThunk(nullptr)
  27. , 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. mRuntimeType = 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* object)
  109. {
  110. mFullTypeName = mNamespace + "." + mTypeName;
  111. mManagedInstance = object;
  112. MonoClass* managedClass = nullptr;
  113. if (mManagedInstance != nullptr)
  114. {
  115. mManagedHandle = MonoUtil::newGCHandle(mManagedInstance);
  116. ::MonoClass* monoClass = MonoUtil::getClass(object);
  117. mRuntimeType = MonoUtil::getType(monoClass);
  118. managedClass = MonoManager::instance().findClass(monoClass);
  119. }
  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(managedClass != nullptr)
  129. {
  130. if (mOnInitializedThunk == nullptr)
  131. {
  132. MonoMethod* onInitializedMethod = managedClass->getMethod("OnInitialize", 0);
  133. if (onInitializedMethod != nullptr)
  134. mOnInitializedThunk = (OnInitializedThunkDef)onInitializedMethod->getThunk();
  135. }
  136. if (mOnUpdateThunk == nullptr)
  137. {
  138. MonoMethod* onUpdateMethod = managedClass->getMethod("OnUpdate", 0);
  139. if (onUpdateMethod != nullptr)
  140. mOnUpdateThunk = (OnUpdateThunkDef)onUpdateMethod->getThunk();
  141. }
  142. if (mOnResetThunk == nullptr)
  143. {
  144. MonoMethod* onResetMethod = managedClass->getMethod("OnReset", 0);
  145. if (onResetMethod != nullptr)
  146. mOnResetThunk = (OnResetThunkDef)onResetMethod->getThunk();
  147. }
  148. if (mOnDestroyThunk == nullptr)
  149. {
  150. MonoMethod* onDestroyMethod = managedClass->getMethod("OnDestroy", 0);
  151. if (onDestroyMethod != nullptr)
  152. mOnDestroyThunk = (OnDestroyedThunkDef)onDestroyMethod->getThunk();
  153. }
  154. if (mOnDisabledThunk == nullptr)
  155. {
  156. MonoMethod* onDisableMethod = managedClass->getMethod("OnDisable", 0);
  157. if (onDisableMethod != nullptr)
  158. mOnDisabledThunk = (OnDisabledThunkDef)onDisableMethod->getThunk();
  159. }
  160. if (mOnEnabledThunk == nullptr)
  161. {
  162. MonoMethod* onEnableMethod = managedClass->getMethod("OnEnable", 0);
  163. if (onEnableMethod != nullptr)
  164. mOnEnabledThunk = (OnInitializedThunkDef)onEnableMethod->getThunk();
  165. }
  166. if (mOnTransformChangedThunk == nullptr)
  167. {
  168. MonoMethod* onTransformChangedMethod = managedClass->getMethod("OnTransformChanged", 1);
  169. if (onTransformChangedMethod != nullptr)
  170. mOnTransformChangedThunk = (OnTransformChangedThunkDef)onTransformChangedMethod->getThunk();
  171. }
  172. if(mCalculateBoundsMethod == nullptr)
  173. mCalculateBoundsMethod = managedClass->getMethod("CalculateBounds", 2);
  174. // Search for methods on base class if there is one
  175. MonoClass* baseClass = managedClass->getBaseClass();
  176. if (baseClass != ScriptComponent::getMetaData()->scriptClass)
  177. managedClass = baseClass;
  178. else
  179. break;
  180. }
  181. if (managedClass != nullptr)
  182. {
  183. MonoAssembly* bansheeEngineAssembly = MonoManager::instance().getAssembly(ENGINE_ASSEMBLY);
  184. if (bansheeEngineAssembly == nullptr)
  185. BS_EXCEPT(InvalidStateException, String(ENGINE_ASSEMBLY) + " assembly is not loaded.");
  186. MonoClass* runInEditorAttrib = bansheeEngineAssembly->getClass("BansheeEngine", "RunInEditor");
  187. if (runInEditorAttrib == nullptr)
  188. BS_EXCEPT(InvalidStateException, "Cannot find RunInEditor managed class.");
  189. mRunInEditor = managedClass->getAttribute(runInEditorAttrib) != nullptr;
  190. }
  191. else
  192. mRunInEditor = false;
  193. }
  194. bool ManagedComponent::typeEquals(const Component& other)
  195. {
  196. if(Component::typeEquals(other))
  197. {
  198. const ManagedComponent& otherMC = static_cast<const ManagedComponent&>(other);
  199. // Not comparing MonoReflectionType directly because this needs to be able to work before instantiation
  200. return mNamespace == otherMC.getManagedNamespace() && mTypeName == otherMC.getManagedTypeName();
  201. }
  202. return false;
  203. }
  204. bool ManagedComponent::calculateBounds(Bounds& bounds)
  205. {
  206. if (mManagedInstance != nullptr && mCalculateBoundsMethod != nullptr)
  207. {
  208. AABox box;
  209. Sphere sphere;
  210. void* params[2];
  211. params[0] = &box;
  212. params[1] = &sphere;
  213. MonoObject* areBoundsValidObj = mCalculateBoundsMethod->invokeVirtual(mManagedInstance, params);
  214. bool areBoundsValid;
  215. areBoundsValid = *(bool*)MonoUtil::unbox(areBoundsValidObj);
  216. bounds = Bounds(box, sphere);
  217. return areBoundsValid;
  218. }
  219. return Component::calculateBounds(bounds);
  220. }
  221. void ManagedComponent::update()
  222. {
  223. if (PlayInEditorManager::instance().getState() != PlayInEditorState::Playing && !mRunInEditor)
  224. return;
  225. assert(mManagedInstance != nullptr);
  226. if (mOnUpdateThunk != nullptr)
  227. {
  228. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  229. // for some extra speed.
  230. MonoUtil::invokeThunk(mOnUpdateThunk, mManagedInstance);
  231. }
  232. }
  233. void ManagedComponent::triggerOnInitialize()
  234. {
  235. if (PlayInEditorManager::instance().getState() == PlayInEditorState::Stopped && !mRunInEditor)
  236. return;
  237. if (mOnInitializedThunk != nullptr)
  238. {
  239. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  240. // for some extra speed.
  241. MonoUtil::invokeThunk(mOnInitializedThunk, mManagedInstance);
  242. }
  243. }
  244. void ManagedComponent::triggerOnReset()
  245. {
  246. assert(mManagedInstance != nullptr);
  247. if (mRequiresReset && mOnResetThunk != nullptr)
  248. {
  249. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  250. // for some extra speed.
  251. MonoUtil::invokeThunk(mOnResetThunk, mManagedInstance);
  252. }
  253. mRequiresReset = false;
  254. }
  255. void ManagedComponent::triggerOnEnable()
  256. {
  257. if (PlayInEditorManager::instance().getState() == PlayInEditorState::Stopped && !mRunInEditor)
  258. return;
  259. assert(mManagedInstance != nullptr);
  260. if (mOnEnabledThunk != nullptr)
  261. {
  262. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  263. // for some extra speed.
  264. MonoUtil::invokeThunk(mOnEnabledThunk, mManagedInstance);
  265. }
  266. }
  267. void ManagedComponent::instantiate()
  268. {
  269. mObjInfo = nullptr;
  270. if (!ScriptAssemblyManager::instance().getSerializableObjectInfo(mNamespace, mTypeName, mObjInfo))
  271. {
  272. MonoObject* instance = ScriptAssemblyManager::instance().getMissingComponentClass()->createInstance(true);
  273. initialize(instance);
  274. mMissingType = true;
  275. }
  276. else
  277. {
  278. initialize(mObjInfo->mMonoClass->createInstance());
  279. mMissingType = false;
  280. }
  281. assert(mManagedInstance != nullptr);
  282. // Find handle to self
  283. HManagedComponent componentHandle;
  284. if (SO() != nullptr)
  285. {
  286. const Vector<HComponent>& components = SO()->getComponents();
  287. for (auto& component : components)
  288. {
  289. if (component.get() == this)
  290. {
  291. componentHandle = component;
  292. break;
  293. }
  294. }
  295. }
  296. assert(componentHandle != nullptr);
  297. ScriptGameObjectManager::instance().createScriptComponent(mManagedInstance, componentHandle);
  298. }
  299. void ManagedComponent::onInitialized()
  300. {
  301. assert(mManagedInstance != nullptr);
  302. if (mSerializedObjectData != nullptr && !mMissingType)
  303. {
  304. mSerializedObjectData->deserialize(mManagedInstance, mObjInfo);
  305. mSerializedObjectData = nullptr;
  306. }
  307. triggerOnInitialize();
  308. triggerOnReset();
  309. }
  310. void ManagedComponent::onDestroyed()
  311. {
  312. assert(mManagedInstance != nullptr);
  313. if (mOnDestroyThunk != nullptr)
  314. {
  315. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  316. // for some extra speed.
  317. MonoUtil::invokeThunk(mOnDestroyThunk, mManagedInstance);
  318. }
  319. mManagedInstance = nullptr;
  320. MonoUtil::freeGCHandle(mManagedHandle);
  321. }
  322. void ManagedComponent::onEnabled()
  323. {
  324. triggerOnEnable();
  325. }
  326. void ManagedComponent::onDisabled()
  327. {
  328. if (PlayInEditorManager::instance().getState() == PlayInEditorState::Stopped && !mRunInEditor)
  329. return;
  330. assert(mManagedInstance != nullptr);
  331. if (mOnDisabledThunk != nullptr)
  332. {
  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, mManagedInstance);
  336. }
  337. }
  338. void ManagedComponent::onTransformChanged(TransformChangedFlags flags)
  339. {
  340. if (PlayInEditorManager::instance().getState() == PlayInEditorState::Stopped && !mRunInEditor)
  341. return;
  342. if(mOnTransformChangedThunk != nullptr)
  343. {
  344. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  345. // for some extra speed.
  346. MonoUtil::invokeThunk(mOnTransformChangedThunk, mManagedInstance, flags);
  347. }
  348. }
  349. RTTITypeBase* ManagedComponent::getRTTIStatic()
  350. {
  351. return ManagedComponentRTTI::instance();
  352. }
  353. RTTITypeBase* ManagedComponent::getRTTI() const
  354. {
  355. return ManagedComponent::getRTTIStatic();
  356. }
  357. }