BsManagedComponent.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. #include "BsManagedComponent.h"
  2. #include "BsManagedComponentRTTI.h"
  3. #include "BsMonoManager.h"
  4. #include "BsMonoClass.h"
  5. #include "BsMonoUtil.h"
  6. #include "BsMonoMethod.h"
  7. #include "BsMemorySerializer.h"
  8. #include "BsManagedSerializableObject.h"
  9. #include "BsScriptGameObjectManager.h"
  10. #include "BsScriptAssemblyManager.h"
  11. #include "BsMonoAssembly.h"
  12. #include "BsPlayInEditorManager.h"
  13. #include "BsDebug.h"
  14. namespace BansheeEngine
  15. {
  16. ManagedComponent::ManagedComponent()
  17. :mManagedInstance(nullptr), mUpdateThunk(nullptr), mOnDestroyThunk(nullptr), mOnInitializedThunk(nullptr),
  18. mOnResetThunk(nullptr), mMissingType(false), mRequiresReset(true), mOnEnabledThunk(nullptr), mOnDisabledThunk(nullptr),
  19. mCalculateBoundsMethod(nullptr), mRunInEditor(false)
  20. { }
  21. ManagedComponent::ManagedComponent(const HSceneObject& parent, MonoReflectionType* runtimeType)
  22. : Component(parent), mManagedInstance(nullptr), mRuntimeType(runtimeType), mUpdateThunk(nullptr),
  23. mOnDestroyThunk(nullptr), mOnInitializedThunk(nullptr), mOnResetThunk(nullptr), mMissingType(false),
  24. mRequiresReset(true), mOnEnabledThunk(nullptr), mOnDisabledThunk(nullptr), mCalculateBoundsMethod(nullptr),
  25. mRunInEditor(false)
  26. {
  27. MonoType* monoType = mono_reflection_type_get_type(mRuntimeType);
  28. ::MonoClass* monoClass = mono_type_get_class(monoType);
  29. MonoUtil::getClassName(monoClass, mNamespace, mTypeName);
  30. setName(mTypeName);
  31. }
  32. ManagedComponent::~ManagedComponent()
  33. {
  34. }
  35. ComponentBackupData ManagedComponent::backup(bool clearExisting)
  36. {
  37. ComponentBackupData backupData;
  38. // If type is not missing read data from actual managed instance, instead just
  39. // return the data we backed up before the type was lost
  40. if (!mMissingType)
  41. {
  42. ManagedSerializableObjectPtr serializableObject = ManagedSerializableObject::createFromExisting(mManagedInstance);
  43. // Serialize the object information and its fields. We cannot just serialize the entire object because
  44. // the managed instance had to be created in a previous step. So we handle creation of the top level object manually.
  45. if (serializableObject != nullptr)
  46. {
  47. MemorySerializer ms;
  48. backupData.size = 0;
  49. backupData.data = ms.encode(serializableObject.get(), backupData.size);
  50. }
  51. else
  52. {
  53. backupData.size = 0;
  54. backupData.data = nullptr;
  55. }
  56. }
  57. else
  58. {
  59. MemorySerializer ms;
  60. backupData.size = 0;
  61. if (mSerializedObjectData != nullptr)
  62. backupData.data = ms.encode(mSerializedObjectData.get(), backupData.size);
  63. else
  64. backupData.data = nullptr;
  65. }
  66. if (clearExisting)
  67. {
  68. if (mManagedInstance != nullptr)
  69. {
  70. mManagedInstance = nullptr;
  71. mono_gchandle_free(mManagedHandle);
  72. mManagedHandle = 0;
  73. }
  74. mRuntimeType = nullptr;
  75. mOnInitializedThunk = nullptr;
  76. mUpdateThunk = nullptr;
  77. mOnDestroyThunk = nullptr;
  78. mOnEnabledThunk = nullptr;
  79. mOnDisabledThunk = nullptr;
  80. mCalculateBoundsMethod = nullptr;
  81. }
  82. return backupData;
  83. }
  84. void ManagedComponent::restore(MonoObject* instance, const ComponentBackupData& data, bool missingType)
  85. {
  86. initialize(instance);
  87. mObjInfo = nullptr;
  88. if (instance != nullptr && data.data != nullptr)
  89. {
  90. MemorySerializer ms;
  91. GameObjectManager::instance().startDeserialization();
  92. ManagedSerializableObjectPtr serializableObject = std::static_pointer_cast<ManagedSerializableObject>(ms.decode(data.data, data.size));
  93. GameObjectManager::instance().endDeserialization();
  94. if (!missingType)
  95. {
  96. ScriptAssemblyManager::instance().getSerializableObjectInfo(mNamespace, mTypeName, mObjInfo);
  97. serializableObject->deserialize(instance, mObjInfo);
  98. }
  99. else
  100. mSerializedObjectData = serializableObject;
  101. }
  102. if (!missingType)
  103. mSerializedObjectData = nullptr;
  104. mMissingType = missingType;
  105. mRequiresReset = true;
  106. }
  107. void ManagedComponent::initialize(MonoObject* object)
  108. {
  109. mFullTypeName = mNamespace + "." + mTypeName;
  110. mManagedInstance = object;
  111. MonoClass* managedClass = nullptr;
  112. if (mManagedInstance != nullptr)
  113. {
  114. mManagedHandle = mono_gchandle_new(mManagedInstance, false);
  115. ::MonoClass* monoClass = mono_object_get_class(object);
  116. MonoType* monoType = mono_class_get_type(monoClass);
  117. mRuntimeType = mono_type_get_object(MonoManager::instance().getDomain(), monoType);
  118. managedClass = MonoManager::instance().findClass(monoClass);
  119. }
  120. if (managedClass != nullptr)
  121. {
  122. MonoMethod* onInitializedMethod = managedClass->getMethod("OnInitialize", 0);
  123. if (onInitializedMethod != nullptr)
  124. mOnInitializedThunk = (OnInitializedThunkDef)onInitializedMethod->getThunk();
  125. MonoMethod* updateMethod = managedClass->getMethod("Update", 0);
  126. if (updateMethod != nullptr)
  127. mUpdateThunk = (UpdateThunkDef)updateMethod->getThunk();
  128. MonoMethod* onResetMethod = managedClass->getMethod("OnReset", 0);
  129. if (onResetMethod != nullptr)
  130. mOnResetThunk = (OnResetThunkDef)onResetMethod->getThunk();
  131. MonoMethod* onDestroyMethod = managedClass->getMethod("OnDestroy", 0);
  132. if (onDestroyMethod != nullptr)
  133. mOnDestroyThunk = (OnDestroyedThunkDef)onDestroyMethod->getThunk();
  134. MonoMethod* onDisableMethod = managedClass->getMethod("OnDisable", 0);
  135. if (onDisableMethod != nullptr)
  136. mOnDisabledThunk = (OnDisabledThunkDef)onDisableMethod->getThunk();
  137. MonoMethod* onEnableMethod = managedClass->getMethod("OnEnable", 0);
  138. if (onEnableMethod != nullptr)
  139. mOnEnabledThunk = (OnInitializedThunkDef)onEnableMethod->getThunk();
  140. mCalculateBoundsMethod = managedClass->getMethod("CalculateBounds", 2);
  141. MonoAssembly* bansheeEngineAssembly = MonoManager::instance().getAssembly(ENGINE_ASSEMBLY);
  142. if (bansheeEngineAssembly == nullptr)
  143. BS_EXCEPT(InvalidStateException, String(ENGINE_ASSEMBLY) + " assembly is not loaded.");
  144. MonoClass* runInEditorAttrib = bansheeEngineAssembly->getClass("BansheeEngine", "RunInEditor");
  145. if (runInEditorAttrib == nullptr)
  146. BS_EXCEPT(InvalidStateException, "Cannot find RunInEditor managed class.");
  147. mRunInEditor = managedClass->getAttribute(runInEditorAttrib) != nullptr;
  148. }
  149. else
  150. mRunInEditor = false;
  151. }
  152. bool ManagedComponent::typeEquals(const Component& other)
  153. {
  154. if(Component::typeEquals(other))
  155. {
  156. const ManagedComponent& otherMC = static_cast<const ManagedComponent&>(other);
  157. // Not comparing MonoReflectionType directly because this needs to be able to work before instantiation
  158. return mNamespace == otherMC.getManagedNamespace() && mTypeName == otherMC.getManagedTypeName();
  159. }
  160. return false;
  161. }
  162. bool ManagedComponent::calculateBounds(Bounds& bounds)
  163. {
  164. if (mManagedInstance != nullptr && mCalculateBoundsMethod != nullptr)
  165. {
  166. AABox box;
  167. Sphere sphere;
  168. void* params[2];
  169. params[0] = &box;
  170. params[1] = &sphere;
  171. MonoObject* areBoundsValidObj = mCalculateBoundsMethod->invokeVirtual(mManagedInstance, params);
  172. bool areBoundsValid;
  173. areBoundsValid = *(bool*)mono_object_unbox(areBoundsValidObj);
  174. bounds = Bounds(box, sphere);
  175. return areBoundsValid;
  176. }
  177. return Component::calculateBounds(bounds);
  178. }
  179. void ManagedComponent::update()
  180. {
  181. if (PlayInEditorManager::instance().getState() != PlayInEditorState::Playing && !mRunInEditor)
  182. return;
  183. assert(mManagedInstance != nullptr);
  184. if (mUpdateThunk != nullptr)
  185. {
  186. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  187. // for some extra speed.
  188. MonoUtil::invokeThunk(mUpdateThunk, mManagedInstance);
  189. }
  190. }
  191. void ManagedComponent::triggerOnReset()
  192. {
  193. assert(mManagedInstance != nullptr);
  194. if (PlayInEditorManager::instance().getState() == PlayInEditorState::Playing || mRunInEditor)
  195. {
  196. if (mRequiresReset && mOnResetThunk != nullptr)
  197. {
  198. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  199. // for some extra speed.
  200. MonoUtil::invokeThunk(mOnResetThunk, mManagedInstance);
  201. }
  202. }
  203. mRequiresReset = false;
  204. }
  205. void ManagedComponent::instantiate()
  206. {
  207. mObjInfo = nullptr;
  208. if (!ScriptAssemblyManager::instance().getSerializableObjectInfo(mNamespace, mTypeName, mObjInfo))
  209. {
  210. MonoObject* instance = ScriptAssemblyManager::instance().getMissingComponentClass()->createInstance(true);
  211. initialize(instance);
  212. mMissingType = true;
  213. }
  214. else
  215. {
  216. initialize(mObjInfo->mMonoClass->createInstance());
  217. mMissingType = false;
  218. }
  219. assert(mManagedInstance != nullptr);
  220. // Find handle to self
  221. HManagedComponent componentHandle;
  222. if (mParent != nullptr)
  223. {
  224. const Vector<HComponent>& components = mParent->getComponents();
  225. for (auto& component : components)
  226. {
  227. if (component.get() == this)
  228. {
  229. componentHandle = component;
  230. break;
  231. }
  232. }
  233. }
  234. assert(componentHandle != nullptr);
  235. ScriptComponent* nativeInstance = ScriptGameObjectManager::instance().createScriptComponent(mManagedInstance, componentHandle);
  236. }
  237. void ManagedComponent::onInitialized()
  238. {
  239. assert(mManagedInstance != nullptr);
  240. if (mSerializedObjectData != nullptr && !mMissingType)
  241. {
  242. mSerializedObjectData->deserialize(mManagedInstance, mObjInfo);
  243. mSerializedObjectData = nullptr;
  244. }
  245. if (PlayInEditorManager::instance().getState() == PlayInEditorState::Playing || mRunInEditor)
  246. {
  247. if (mOnInitializedThunk != nullptr)
  248. {
  249. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  250. // for some extra speed.
  251. MonoUtil::invokeThunk(mOnInitializedThunk, mManagedInstance);
  252. }
  253. }
  254. triggerOnReset();
  255. }
  256. void ManagedComponent::onDestroyed()
  257. {
  258. assert(mManagedInstance != nullptr);
  259. if (PlayInEditorManager::instance().getState() == PlayInEditorState::Playing || mRunInEditor)
  260. {
  261. if (mOnDestroyThunk != nullptr)
  262. {
  263. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  264. // for some extra speed.
  265. MonoUtil::invokeThunk(mOnDestroyThunk, mManagedInstance);
  266. }
  267. }
  268. mManagedInstance = nullptr;
  269. mono_gchandle_free(mManagedHandle);
  270. }
  271. void ManagedComponent::onEnabled()
  272. {
  273. if (PlayInEditorManager::instance().getState() != PlayInEditorState::Playing && !mRunInEditor)
  274. return;
  275. assert(mManagedInstance != nullptr);
  276. if (mOnEnabledThunk != nullptr)
  277. {
  278. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  279. // for some extra speed.
  280. MonoUtil::invokeThunk(mOnEnabledThunk, mManagedInstance);
  281. }
  282. }
  283. void ManagedComponent::onDisabled()
  284. {
  285. if (PlayInEditorManager::instance().getState() != PlayInEditorState::Playing && !mRunInEditor)
  286. return;
  287. assert(mManagedInstance != nullptr);
  288. if (mOnDisabledThunk != nullptr)
  289. {
  290. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  291. // for some extra speed.
  292. MonoUtil::invokeThunk(mOnDisabledThunk, mManagedInstance);
  293. }
  294. }
  295. RTTITypeBase* ManagedComponent::getRTTIStatic()
  296. {
  297. return ManagedComponentRTTI::instance();
  298. }
  299. RTTITypeBase* ManagedComponent::getRTTI() const
  300. {
  301. return ManagedComponent::getRTTIStatic();
  302. }
  303. }