BsManagedComponent.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsManagedComponent.h"
  4. #include "RTTI/BsManagedComponentRTTI.h"
  5. #include "BsMonoManager.h"
  6. #include "BsMonoClass.h"
  7. #include "BsMonoUtil.h"
  8. #include "BsMonoMethod.h"
  9. #include "Serialization/BsMemorySerializer.h"
  10. #include "Serialization/BsManagedSerializableObject.h"
  11. #include "BsScriptGameObjectManager.h"
  12. #include "Serialization/BsScriptAssemblyManager.h"
  13. #include "Wrappers/BsScriptManagedComponent.h"
  14. #include "BsMonoAssembly.h"
  15. #include "BsPlayInEditorManager.h"
  16. namespace bs
  17. {
  18. ManagedComponent::ManagedComponent()
  19. : mManagedInstance(nullptr), mManagedClass(nullptr), mRuntimeType(nullptr), mManagedHandle(0)
  20. , mRequiresReset(true), mMissingType(false), mOnCreatedThunk(nullptr), mOnInitializedThunk(nullptr)
  21. , mOnUpdateThunk(nullptr), mOnResetThunk(nullptr), mOnDestroyThunk(nullptr), mOnDisabledThunk(nullptr)
  22. , mOnEnabledThunk(nullptr), mOnTransformChangedThunk(nullptr), mCalculateBoundsMethod(nullptr)
  23. { }
  24. ManagedComponent::ManagedComponent(const HSceneObject& parent, MonoReflectionType* runtimeType)
  25. : Component(parent), mManagedInstance(nullptr), mManagedClass(nullptr), mRuntimeType(runtimeType)
  26. , mManagedHandle(0), mRequiresReset(true), mMissingType(false), mOnCreatedThunk(nullptr)
  27. , mOnInitializedThunk(nullptr), mOnUpdateThunk(nullptr), mOnResetThunk(nullptr), mOnDestroyThunk(nullptr)
  28. , mOnDisabledThunk(nullptr), mOnEnabledThunk(nullptr), mOnTransformChangedThunk(nullptr)
  29. , mCalculateBoundsMethod(nullptr)
  30. {
  31. MonoUtil::getClassName(mRuntimeType, 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. SPtr<ManagedSerializableObject> 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. MonoUtil::freeGCHandle(mManagedHandle);
  74. mManagedHandle = 0;
  75. }
  76. mManagedClass = nullptr;
  77. mRuntimeType = nullptr;
  78. mOnCreatedThunk = nullptr;
  79. mOnInitializedThunk = nullptr;
  80. mOnUpdateThunk = nullptr;
  81. mOnDestroyThunk = nullptr;
  82. mOnEnabledThunk = nullptr;
  83. mOnDisabledThunk = nullptr;
  84. mOnTransformChangedThunk = nullptr;
  85. mCalculateBoundsMethod = nullptr;
  86. }
  87. return backupData;
  88. }
  89. void ManagedComponent::restore(MonoObject* instance, const ComponentBackupData& data, bool missingType)
  90. {
  91. initialize(instance);
  92. mObjInfo = nullptr;
  93. if (instance != nullptr && data.data != nullptr)
  94. {
  95. MemorySerializer ms;
  96. GameObjectManager::instance().startDeserialization();
  97. SPtr<ManagedSerializableObject> serializableObject = std::static_pointer_cast<ManagedSerializableObject>(ms.decode(data.data, data.size));
  98. GameObjectManager::instance().endDeserialization();
  99. if (!missingType)
  100. {
  101. ScriptAssemblyManager::instance().getSerializableObjectInfo(mNamespace, mTypeName, mObjInfo);
  102. serializableObject->deserialize(instance, mObjInfo);
  103. }
  104. else
  105. mSerializedObjectData = serializableObject;
  106. }
  107. if (!missingType)
  108. mSerializedObjectData = nullptr;
  109. mMissingType = missingType;
  110. mRequiresReset = true;
  111. }
  112. void ManagedComponent::initialize(MonoObject* object)
  113. {
  114. mFullTypeName = mNamespace + "." + mTypeName;
  115. mManagedInstance = object;
  116. mManagedClass = nullptr;
  117. if (mManagedInstance != nullptr)
  118. {
  119. mManagedHandle = MonoUtil::newGCHandle(mManagedInstance);
  120. mManagedInstance = MonoUtil::getObjectFromGCHandle(mManagedHandle);
  121. ::MonoClass* monoClass = MonoUtil::getClass(mManagedInstance);
  122. mRuntimeType = MonoUtil::getType(monoClass);
  123. mManagedClass = MonoManager::instance().findClass(monoClass);
  124. }
  125. mOnCreatedThunk = nullptr;
  126. mOnInitializedThunk = nullptr;
  127. mOnUpdateThunk = nullptr;
  128. mOnResetThunk = nullptr;
  129. mOnDestroyThunk = nullptr;
  130. mOnDisabledThunk = nullptr;
  131. mOnEnabledThunk = nullptr;
  132. mOnTransformChangedThunk = nullptr;
  133. mCalculateBoundsMethod = nullptr;
  134. while(mManagedClass != nullptr)
  135. {
  136. if (mOnCreatedThunk == nullptr)
  137. {
  138. MonoMethod* onCreatedMethod = mManagedClass->getMethod("OnCreate", 0);
  139. if (onCreatedMethod != nullptr)
  140. mOnCreatedThunk = (OnInitializedThunkDef)onCreatedMethod->getThunk();
  141. }
  142. if (mOnInitializedThunk == nullptr)
  143. {
  144. MonoMethod* onInitializedMethod = mManagedClass->getMethod("OnInitialize", 0);
  145. if (onInitializedMethod != nullptr)
  146. mOnInitializedThunk = (OnInitializedThunkDef)onInitializedMethod->getThunk();
  147. }
  148. if (mOnUpdateThunk == nullptr)
  149. {
  150. MonoMethod* onUpdateMethod = mManagedClass->getMethod("OnUpdate", 0);
  151. if (onUpdateMethod != nullptr)
  152. mOnUpdateThunk = (OnUpdateThunkDef)onUpdateMethod->getThunk();
  153. }
  154. if (mOnResetThunk == nullptr)
  155. {
  156. MonoMethod* onResetMethod = mManagedClass->getMethod("OnReset", 0);
  157. if (onResetMethod != nullptr)
  158. mOnResetThunk = (OnResetThunkDef)onResetMethod->getThunk();
  159. }
  160. if (mOnDestroyThunk == nullptr)
  161. {
  162. MonoMethod* onDestroyMethod = mManagedClass->getMethod("OnDestroy", 0);
  163. if (onDestroyMethod != nullptr)
  164. mOnDestroyThunk = (OnDestroyedThunkDef)onDestroyMethod->getThunk();
  165. }
  166. if (mOnDisabledThunk == nullptr)
  167. {
  168. MonoMethod* onDisableMethod = mManagedClass->getMethod("OnDisable", 0);
  169. if (onDisableMethod != nullptr)
  170. mOnDisabledThunk = (OnDisabledThunkDef)onDisableMethod->getThunk();
  171. }
  172. if (mOnEnabledThunk == nullptr)
  173. {
  174. MonoMethod* onEnableMethod = mManagedClass->getMethod("OnEnable", 0);
  175. if (onEnableMethod != nullptr)
  176. mOnEnabledThunk = (OnInitializedThunkDef)onEnableMethod->getThunk();
  177. }
  178. if (mOnTransformChangedThunk == nullptr)
  179. {
  180. MonoMethod* onTransformChangedMethod = mManagedClass->getMethod("OnTransformChanged", 1);
  181. if (onTransformChangedMethod != nullptr)
  182. mOnTransformChangedThunk = (OnTransformChangedThunkDef)onTransformChangedMethod->getThunk();
  183. }
  184. if(mCalculateBoundsMethod == nullptr)
  185. mCalculateBoundsMethod = mManagedClass->getMethod("CalculateBounds", 2);
  186. // Search for methods on base class if there is one
  187. MonoClass* baseClass = mManagedClass->getBaseClass();
  188. if (baseClass != ScriptManagedComponent::getMetaData()->scriptClass)
  189. mManagedClass = baseClass;
  190. else
  191. break;
  192. }
  193. if (mManagedClass != nullptr)
  194. {
  195. MonoAssembly* bansheeEngineAssembly = MonoManager::instance().getAssembly(ENGINE_ASSEMBLY);
  196. if (bansheeEngineAssembly == nullptr)
  197. BS_EXCEPT(InvalidStateException, String(ENGINE_ASSEMBLY) + " assembly is not loaded.");
  198. MonoClass* runInEditorAttrib = bansheeEngineAssembly->getClass("BansheeEngine", "RunInEditor");
  199. if (runInEditorAttrib == nullptr)
  200. BS_EXCEPT(InvalidStateException, "Cannot find RunInEditor managed class.");
  201. bool runInEditor = mManagedClass->getAttribute(runInEditorAttrib) != nullptr;
  202. if (runInEditor)
  203. setFlag(ComponentFlag::AlwaysRun, true);
  204. }
  205. }
  206. bool ManagedComponent::typeEquals(const Component& other)
  207. {
  208. if(Component::typeEquals(other))
  209. {
  210. const ManagedComponent& otherMC = static_cast<const ManagedComponent&>(other);
  211. // Not comparing MonoReflectionType directly because this needs to be able to work before instantiation
  212. return mNamespace == otherMC.getManagedNamespace() && mTypeName == otherMC.getManagedTypeName();
  213. }
  214. return false;
  215. }
  216. bool ManagedComponent::calculateBounds(Bounds& bounds)
  217. {
  218. if (mManagedInstance != nullptr && mCalculateBoundsMethod != nullptr)
  219. {
  220. AABox box;
  221. Sphere sphere;
  222. void* params[2];
  223. params[0] = &box;
  224. params[1] = &sphere;
  225. MonoObject* areBoundsValidObj = mCalculateBoundsMethod->invokeVirtual(mManagedInstance, params);
  226. bool areBoundsValid;
  227. areBoundsValid = *(bool*)MonoUtil::unbox(areBoundsValidObj);
  228. bounds = Bounds(box, sphere);
  229. return areBoundsValid;
  230. }
  231. return Component::calculateBounds(bounds);
  232. }
  233. void ManagedComponent::update()
  234. {
  235. assert(mManagedInstance != nullptr);
  236. if (mOnUpdateThunk != nullptr)
  237. {
  238. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  239. // for some extra speed.
  240. MonoUtil::invokeThunk(mOnUpdateThunk, mManagedInstance);
  241. }
  242. }
  243. void ManagedComponent::triggerOnReset()
  244. {
  245. assert(mManagedInstance != nullptr);
  246. if (mRequiresReset && mOnResetThunk != nullptr)
  247. {
  248. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  249. // for some extra speed.
  250. MonoUtil::invokeThunk(mOnResetThunk, mManagedInstance);
  251. }
  252. mRequiresReset = false;
  253. }
  254. void ManagedComponent::_instantiate()
  255. {
  256. mObjInfo = nullptr;
  257. if (!ScriptAssemblyManager::instance().getSerializableObjectInfo(mNamespace, mTypeName, mObjInfo))
  258. {
  259. MonoObject* instance = ScriptAssemblyManager::instance().getMissingComponentClass()->createInstance(true);
  260. initialize(instance);
  261. mMissingType = true;
  262. }
  263. else
  264. {
  265. initialize(mObjInfo->mMonoClass->createInstance());
  266. mMissingType = false;
  267. }
  268. assert(mManagedInstance != nullptr);
  269. // Find handle to self
  270. HManagedComponent componentHandle;
  271. if (SO() != nullptr)
  272. {
  273. const Vector<HComponent>& components = SO()->getComponents();
  274. for (auto& component : components)
  275. {
  276. if (component.get() == this)
  277. {
  278. componentHandle = component;
  279. break;
  280. }
  281. }
  282. }
  283. assert(componentHandle != nullptr);
  284. ScriptGameObjectManager::instance().createManagedScriptComponent(mManagedInstance, componentHandle);
  285. }
  286. void ManagedComponent::onCreated()
  287. {
  288. assert(mManagedInstance != nullptr);
  289. if (mSerializedObjectData != nullptr && !mMissingType)
  290. {
  291. mSerializedObjectData->deserialize(mManagedInstance, mObjInfo);
  292. mSerializedObjectData = nullptr;
  293. }
  294. if (mOnCreatedThunk != nullptr)
  295. {
  296. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  297. // for some extra speed.
  298. MonoUtil::invokeThunk(mOnCreatedThunk, mManagedInstance);
  299. }
  300. triggerOnReset();
  301. }
  302. void ManagedComponent::onInitialized()
  303. {
  304. assert(mManagedInstance != nullptr);
  305. if (mOnInitializedThunk != nullptr)
  306. {
  307. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  308. // for some extra speed.
  309. MonoUtil::invokeThunk(mOnInitializedThunk, mManagedInstance);
  310. }
  311. triggerOnReset();
  312. }
  313. void ManagedComponent::onDestroyed()
  314. {
  315. assert(mManagedInstance != nullptr);
  316. if (mOnDestroyThunk != nullptr)
  317. {
  318. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  319. // for some extra speed.
  320. MonoUtil::invokeThunk(mOnDestroyThunk, mManagedInstance);
  321. }
  322. mManagedInstance = nullptr;
  323. MonoUtil::freeGCHandle(mManagedHandle);
  324. }
  325. void ManagedComponent::onEnabled()
  326. {
  327. assert(mManagedInstance != nullptr);
  328. if (mOnEnabledThunk != nullptr)
  329. {
  330. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  331. // for some extra speed.
  332. MonoUtil::invokeThunk(mOnEnabledThunk, mManagedInstance);
  333. }
  334. }
  335. void ManagedComponent::onDisabled()
  336. {
  337. assert(mManagedInstance != nullptr);
  338. if (mOnDisabledThunk != nullptr)
  339. {
  340. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  341. // for some extra speed.
  342. MonoUtil::invokeThunk(mOnDisabledThunk, mManagedInstance);
  343. }
  344. }
  345. void ManagedComponent::onTransformChanged(TransformChangedFlags flags)
  346. {
  347. if(mOnTransformChangedThunk != nullptr)
  348. {
  349. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  350. // for some extra speed.
  351. MonoUtil::invokeThunk(mOnTransformChangedThunk, mManagedInstance, flags);
  352. }
  353. }
  354. RTTITypeBase* ManagedComponent::getRTTIStatic()
  355. {
  356. return ManagedComponentRTTI::instance();
  357. }
  358. RTTITypeBase* ManagedComponent::getRTTI() const
  359. {
  360. return ManagedComponent::getRTTIStatic();
  361. }
  362. }