2
0

BsManagedComponent.cpp 12 KB

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