BsManagedComponent.cpp 13 KB

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