BsManagedComponent.cpp 11 KB

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