2
0

BsManagedComponent.cpp 13 KB

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