BsManagedComponent.cpp 13 KB

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