BsScriptAssemblyManager.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsScriptAssemblyManager.h"
  4. #include "BsManagedSerializableObjectInfo.h"
  5. #include "BsMonoManager.h"
  6. #include "BsMonoAssembly.h"
  7. #include "BsMonoClass.h"
  8. #include "BsMonoField.h"
  9. #include "BsMonoMethod.h"
  10. #include "BsMonoProperty.h"
  11. #include "BsScriptManagedResource.h"
  12. #include "BsScriptTexture2D.h"
  13. #include "BsScriptTexture3D.h"
  14. #include "BsScriptTextureCube.h"
  15. #include "BsScriptSpriteTexture.h"
  16. #include "BsScriptMaterial.h"
  17. #include "BsScriptMesh.h"
  18. #include "BsScriptFont.h"
  19. #include "BsScriptShader.h"
  20. #include "BsScriptShaderInclude.h"
  21. #include "BsScriptPlainText.h"
  22. #include "BsScriptScriptCode.h"
  23. #include "BsScriptStringTable.h"
  24. #include "BsScriptGUISkin.h"
  25. #include "BsScriptPhysicsMaterial.h"
  26. #include "BsScriptPhysicsMesh.h"
  27. #include "BsScriptPrefab.h"
  28. namespace BansheeEngine
  29. {
  30. ScriptAssemblyManager::ScriptAssemblyManager()
  31. :mBaseTypesInitialized(false), mSerializeObjectAttribute(nullptr), mDontSerializeFieldAttribute(nullptr),
  32. mComponentClass(nullptr), mSceneObjectClass(nullptr), mSerializeFieldAttribute(nullptr), mHideInInspectorAttribute(nullptr),
  33. mSystemArrayClass(nullptr), mSystemGenericListClass(nullptr), mSystemGenericDictionaryClass(nullptr), mMissingComponentClass(nullptr),
  34. mSystemTypeClass(nullptr)
  35. {
  36. }
  37. ScriptAssemblyManager::~ScriptAssemblyManager()
  38. {
  39. }
  40. Vector<String> ScriptAssemblyManager::getScriptAssemblies() const
  41. {
  42. Vector<String> initializedAssemblies;
  43. for (auto& assemblyPair : mAssemblyInfos)
  44. initializedAssemblies.push_back(assemblyPair.first);
  45. return initializedAssemblies;
  46. }
  47. void ScriptAssemblyManager::loadAssemblyInfo(const String& assemblyName)
  48. {
  49. if(!mBaseTypesInitialized)
  50. initializeBaseTypes();
  51. // Process all classes and fields
  52. UINT32 mUniqueTypeId = 1;
  53. MonoAssembly* curAssembly = MonoManager::instance().getAssembly(assemblyName);
  54. if(curAssembly == nullptr)
  55. return;
  56. std::shared_ptr<ManagedSerializableAssemblyInfo> assemblyInfo = bs_shared_ptr_new<ManagedSerializableAssemblyInfo>();
  57. assemblyInfo->mName = assemblyName;
  58. mAssemblyInfos[assemblyName] = assemblyInfo;
  59. MonoClass* managedResourceClass = ScriptManagedResource::getMetaData()->scriptClass;
  60. // Populate class data
  61. const Vector<MonoClass*>& allClasses = curAssembly->getAllClasses();
  62. for(auto& curClass : allClasses)
  63. {
  64. if ((curClass->isSubClassOf(mComponentClass) || curClass->isSubClassOf(managedResourceClass) ||
  65. curClass->hasAttribute(mSerializeObjectAttribute)) && curClass != mComponentClass && curClass != managedResourceClass)
  66. {
  67. std::shared_ptr<ManagedSerializableTypeInfoObject> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoObject>();
  68. typeInfo->mTypeNamespace = curClass->getNamespace();
  69. typeInfo->mTypeName = curClass->getTypeName();
  70. typeInfo->mTypeId = mUniqueTypeId++;
  71. MonoType* monoType = mono_class_get_type(curClass->_getInternalClass());
  72. int monoPrimitiveType = mono_type_get_type(monoType);
  73. if(monoPrimitiveType == MONO_TYPE_VALUETYPE)
  74. typeInfo->mValueType = true;
  75. else
  76. typeInfo->mValueType = false;
  77. std::shared_ptr<ManagedSerializableObjectInfo> objInfo = bs_shared_ptr_new<ManagedSerializableObjectInfo>();
  78. objInfo->mTypeInfo = typeInfo;
  79. objInfo->mMonoClass = curClass;
  80. assemblyInfo->mTypeNameToId[objInfo->getFullTypeName()] = typeInfo->mTypeId;
  81. assemblyInfo->mObjectInfos[typeInfo->mTypeId] = objInfo;
  82. }
  83. }
  84. // Populate field data
  85. for(auto& curClassInfo : assemblyInfo->mObjectInfos)
  86. {
  87. std::shared_ptr<ManagedSerializableObjectInfo> objInfo = curClassInfo.second;
  88. UINT32 mUniqueFieldId = 1;
  89. const Vector<MonoField*>& fields = objInfo->mMonoClass->getAllFields();
  90. for(auto& field : fields)
  91. {
  92. if(field->isStatic())
  93. continue;
  94. ManagedSerializableTypeInfoPtr typeInfo = getTypeInfo(field->getType());
  95. if (typeInfo == nullptr)
  96. continue;
  97. std::shared_ptr<ManagedSerializableFieldInfo> fieldInfo = bs_shared_ptr_new<ManagedSerializableFieldInfo>();
  98. fieldInfo->mFieldId = mUniqueFieldId++;
  99. fieldInfo->mName = field->getName();
  100. fieldInfo->mMonoField = field;
  101. fieldInfo->mTypeInfo = typeInfo;
  102. fieldInfo->mParentTypeId = objInfo->mTypeInfo->mTypeId;
  103. MonoFieldVisibility visibility = field->getVisibility();
  104. if (visibility == MonoFieldVisibility::Public)
  105. {
  106. if (!field->hasAttribute(mDontSerializeFieldAttribute))
  107. fieldInfo->mFlags = (ScriptFieldFlags)((UINT32)fieldInfo->mFlags | (UINT32)ScriptFieldFlags::Serializable);
  108. if (!field->hasAttribute(mHideInInspectorAttribute))
  109. fieldInfo->mFlags = (ScriptFieldFlags)((UINT32)fieldInfo->mFlags | (UINT32)ScriptFieldFlags::Inspectable);
  110. }
  111. else
  112. {
  113. if (field->hasAttribute(mSerializeFieldAttribute))
  114. fieldInfo->mFlags = (ScriptFieldFlags)((UINT32)fieldInfo->mFlags | (UINT32)ScriptFieldFlags::Serializable);
  115. }
  116. objInfo->mFieldNameToId[fieldInfo->mName] = fieldInfo->mFieldId;
  117. objInfo->mFields[fieldInfo->mFieldId] = fieldInfo;
  118. }
  119. }
  120. // Form parent/child connections
  121. for(auto& curClass : assemblyInfo->mObjectInfos)
  122. {
  123. MonoClass* base = curClass.second->mMonoClass->getBaseClass();
  124. while(base != nullptr)
  125. {
  126. std::shared_ptr<ManagedSerializableObjectInfo> baseObjInfo;
  127. if(getSerializableObjectInfo(base->getNamespace(), base->getTypeName(), baseObjInfo))
  128. {
  129. curClass.second->mBaseClass = baseObjInfo;
  130. baseObjInfo->mDerivedClasses.push_back(curClass.second);
  131. break;
  132. }
  133. base = base->getBaseClass();
  134. }
  135. }
  136. }
  137. void ScriptAssemblyManager::clearAssemblyInfo()
  138. {
  139. clearScriptObjects();
  140. mAssemblyInfos.clear();
  141. }
  142. ManagedSerializableTypeInfoPtr ScriptAssemblyManager::getTypeInfo(MonoClass* monoClass)
  143. {
  144. if(!mBaseTypesInitialized)
  145. BS_EXCEPT(InvalidStateException, "Calling determineType without previously initializing base types.");
  146. MonoType* monoType = mono_class_get_type(monoClass->_getInternalClass());
  147. int monoPrimitiveType = mono_type_get_type(monoType);
  148. // If enum get the enum base data type
  149. bool isEnum = mono_class_is_enum(monoClass->_getInternalClass()) == 1;
  150. if (isEnum)
  151. {
  152. MonoType* underlyingType = mono_type_get_underlying_type(monoType);
  153. monoPrimitiveType = mono_type_get_type(underlyingType);
  154. }
  155. // Determine field type
  156. switch(monoPrimitiveType)
  157. {
  158. case MONO_TYPE_BOOLEAN:
  159. {
  160. std::shared_ptr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  161. typeInfo->mType = ScriptPrimitiveType::Bool;
  162. return typeInfo;
  163. }
  164. case MONO_TYPE_CHAR:
  165. {
  166. std::shared_ptr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  167. typeInfo->mType = ScriptPrimitiveType::Char;
  168. return typeInfo;
  169. }
  170. case MONO_TYPE_I1:
  171. {
  172. std::shared_ptr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  173. typeInfo->mType = ScriptPrimitiveType::I8;
  174. return typeInfo;
  175. }
  176. case MONO_TYPE_U1:
  177. {
  178. std::shared_ptr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  179. typeInfo->mType = ScriptPrimitiveType::U8;
  180. return typeInfo;
  181. }
  182. case MONO_TYPE_I2:
  183. {
  184. std::shared_ptr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  185. typeInfo->mType = ScriptPrimitiveType::I16;
  186. return typeInfo;
  187. }
  188. case MONO_TYPE_U2:
  189. {
  190. std::shared_ptr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  191. typeInfo->mType = ScriptPrimitiveType::U16;
  192. return typeInfo;
  193. }
  194. case MONO_TYPE_I4:
  195. {
  196. std::shared_ptr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  197. typeInfo->mType = ScriptPrimitiveType::I32;
  198. return typeInfo;
  199. }
  200. case MONO_TYPE_U4:
  201. {
  202. std::shared_ptr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  203. typeInfo->mType = ScriptPrimitiveType::U32;
  204. return typeInfo;
  205. }
  206. case MONO_TYPE_I8:
  207. {
  208. std::shared_ptr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  209. typeInfo->mType = ScriptPrimitiveType::I64;
  210. return typeInfo;
  211. }
  212. case MONO_TYPE_U8:
  213. {
  214. std::shared_ptr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  215. typeInfo->mType = ScriptPrimitiveType::U64;
  216. return typeInfo;
  217. }
  218. case MONO_TYPE_STRING:
  219. {
  220. std::shared_ptr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  221. typeInfo->mType = ScriptPrimitiveType::String;
  222. return typeInfo;
  223. }
  224. case MONO_TYPE_R4:
  225. {
  226. std::shared_ptr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  227. typeInfo->mType = ScriptPrimitiveType::Float;
  228. return typeInfo;
  229. }
  230. case MONO_TYPE_R8:
  231. {
  232. std::shared_ptr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  233. typeInfo->mType = ScriptPrimitiveType::Double;
  234. return typeInfo;
  235. }
  236. case MONO_TYPE_CLASS:
  237. if(monoClass->isSubClassOf(ScriptTexture2D::getMetaData()->scriptClass))
  238. {
  239. std::shared_ptr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  240. typeInfo->mType = ScriptPrimitiveType::Texture2DRef;
  241. return typeInfo;
  242. }
  243. if (monoClass->isSubClassOf(ScriptTexture3D::getMetaData()->scriptClass))
  244. {
  245. std::shared_ptr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  246. typeInfo->mType = ScriptPrimitiveType::Texture3DRef;
  247. return typeInfo;
  248. }
  249. if (monoClass->isSubClassOf(ScriptTextureCube::getMetaData()->scriptClass))
  250. {
  251. std::shared_ptr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  252. typeInfo->mType = ScriptPrimitiveType::TextureCubeRef;
  253. return typeInfo;
  254. }
  255. else if (monoClass->isSubClassOf(ScriptSpriteTexture::getMetaData()->scriptClass))
  256. {
  257. std::shared_ptr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  258. typeInfo->mType = ScriptPrimitiveType::SpriteTextureRef;
  259. return typeInfo;
  260. }
  261. else if (monoClass->isSubClassOf(ScriptManagedResource::getMetaData()->scriptClass))
  262. {
  263. std::shared_ptr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  264. typeInfo->mType = ScriptPrimitiveType::ManagedResourceRef;
  265. return typeInfo;
  266. }
  267. else if (monoClass->isSubClassOf(ScriptShader::getMetaData()->scriptClass))
  268. {
  269. std::shared_ptr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  270. typeInfo->mType = ScriptPrimitiveType::ShaderRef;
  271. return typeInfo;
  272. }
  273. else if (monoClass->isSubClassOf(ScriptShaderInclude::getMetaData()->scriptClass))
  274. {
  275. std::shared_ptr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  276. typeInfo->mType = ScriptPrimitiveType::ShaderIncludeRef;
  277. return typeInfo;
  278. }
  279. else if (monoClass->isSubClassOf(ScriptMaterial::getMetaData()->scriptClass))
  280. {
  281. std::shared_ptr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  282. typeInfo->mType = ScriptPrimitiveType::MaterialRef;
  283. return typeInfo;
  284. }
  285. else if (monoClass->isSubClassOf(ScriptMesh::getMetaData()->scriptClass))
  286. {
  287. std::shared_ptr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  288. typeInfo->mType = ScriptPrimitiveType::MeshRef;
  289. return typeInfo;
  290. }
  291. else if (monoClass->isSubClassOf(ScriptPlainText::getMetaData()->scriptClass))
  292. {
  293. std::shared_ptr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  294. typeInfo->mType = ScriptPrimitiveType::PlainTextRef;
  295. return typeInfo;
  296. }
  297. else if (monoClass->isSubClassOf(ScriptScriptCode::getMetaData()->scriptClass))
  298. {
  299. std::shared_ptr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  300. typeInfo->mType = ScriptPrimitiveType::ScriptCodeRef;
  301. return typeInfo;
  302. }
  303. else if (monoClass->isSubClassOf(ScriptPrefab::getMetaData()->scriptClass))
  304. {
  305. std::shared_ptr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  306. typeInfo->mType = ScriptPrimitiveType::PrefabRef;
  307. return typeInfo;
  308. }
  309. else if (monoClass->isSubClassOf(ScriptFont::getMetaData()->scriptClass))
  310. {
  311. std::shared_ptr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  312. typeInfo->mType = ScriptPrimitiveType::FontRef;
  313. return typeInfo;
  314. }
  315. else if (monoClass->isSubClassOf(ScriptStringTable::getMetaData()->scriptClass))
  316. {
  317. std::shared_ptr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  318. typeInfo->mType = ScriptPrimitiveType::StringTableRef;
  319. return typeInfo;
  320. }
  321. else if (monoClass->isSubClassOf(ScriptGUISkin::getMetaData()->scriptClass))
  322. {
  323. std::shared_ptr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  324. typeInfo->mType = ScriptPrimitiveType::GUISkinRef;
  325. return typeInfo;
  326. }
  327. else if (monoClass->isSubClassOf(ScriptPhysicsMaterial::getMetaData()->scriptClass))
  328. {
  329. std::shared_ptr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  330. typeInfo->mType = ScriptPrimitiveType::PhysicsMaterialRef;
  331. return typeInfo;
  332. }
  333. else if (monoClass->isSubClassOf(ScriptPhysicsMesh::getMetaData()->scriptClass))
  334. {
  335. std::shared_ptr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  336. typeInfo->mType = ScriptPrimitiveType::PhysicsMeshRef;
  337. return typeInfo;
  338. }
  339. else if(monoClass->isSubClassOf(mSceneObjectClass))
  340. {
  341. std::shared_ptr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  342. typeInfo->mType = ScriptPrimitiveType::SceneObjectRef;
  343. return typeInfo;
  344. }
  345. else if(monoClass->isSubClassOf(mComponentClass))
  346. {
  347. std::shared_ptr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  348. typeInfo->mType = ScriptPrimitiveType::ComponentRef;
  349. return typeInfo;
  350. }
  351. else
  352. {
  353. std::shared_ptr<ManagedSerializableObjectInfo> objInfo;
  354. if (getSerializableObjectInfo(monoClass->getNamespace(), monoClass->getTypeName(), objInfo))
  355. return objInfo->mTypeInfo;
  356. }
  357. break;
  358. case MONO_TYPE_VALUETYPE:
  359. {
  360. std::shared_ptr<ManagedSerializableObjectInfo> objInfo;
  361. if (getSerializableObjectInfo(monoClass->getNamespace(), monoClass->getTypeName(), objInfo))
  362. return objInfo->mTypeInfo;
  363. }
  364. break;
  365. case MONO_TYPE_GENERICINST:
  366. if(monoClass->getFullName() == mSystemGenericListClass->getFullName()) // Full name is part of CIL spec, so it is just fine to compare like this
  367. {
  368. std::shared_ptr<ManagedSerializableTypeInfoList> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoList>();
  369. MonoProperty& itemProperty = monoClass->getProperty("Item");
  370. MonoClass* itemClass = itemProperty.getReturnType();
  371. if (itemClass != nullptr)
  372. typeInfo->mElementType = getTypeInfo(itemClass);
  373. if (typeInfo->mElementType == nullptr)
  374. return nullptr;
  375. return typeInfo;
  376. }
  377. else if(monoClass->getFullName() == mSystemGenericDictionaryClass->getFullName())
  378. {
  379. std::shared_ptr<ManagedSerializableTypeInfoDictionary> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoDictionary>();
  380. MonoMethod* getEnumerator = monoClass->getMethod("GetEnumerator");
  381. MonoClass* enumClass = getEnumerator->getReturnType();
  382. MonoProperty& currentProp = enumClass->getProperty("Current");
  383. MonoClass* keyValuePair = currentProp.getReturnType();
  384. MonoProperty& keyProperty = keyValuePair->getProperty("Key");
  385. MonoProperty& valueProperty = keyValuePair->getProperty("Value");
  386. MonoClass* keyClass = keyProperty.getReturnType();
  387. if(keyClass != nullptr)
  388. typeInfo->mKeyType = getTypeInfo(keyClass);
  389. MonoClass* valueClass = valueProperty.getReturnType();
  390. if(valueClass != nullptr)
  391. typeInfo->mValueType = getTypeInfo(valueClass);
  392. if (typeInfo->mKeyType == nullptr || typeInfo->mValueType == nullptr)
  393. return nullptr;
  394. return typeInfo;
  395. }
  396. break;
  397. case MONO_TYPE_SZARRAY:
  398. case MONO_TYPE_ARRAY:
  399. {
  400. std::shared_ptr<ManagedSerializableTypeInfoArray> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoArray>();
  401. ::MonoClass* elementClass = mono_class_get_element_class(monoClass->_getInternalClass());
  402. if(elementClass != nullptr)
  403. {
  404. MonoClass* monoElementClass = MonoManager::instance().findClass(elementClass);
  405. if(monoElementClass != nullptr)
  406. typeInfo->mElementType = getTypeInfo(monoElementClass);
  407. }
  408. if (typeInfo->mElementType == nullptr)
  409. return nullptr;
  410. typeInfo->mRank = (UINT32)mono_class_get_rank(monoClass->_getInternalClass());
  411. return typeInfo;
  412. }
  413. }
  414. return nullptr;
  415. }
  416. void ScriptAssemblyManager::clearScriptObjects()
  417. {
  418. mBaseTypesInitialized = false;
  419. mSystemArrayClass = nullptr;
  420. mSystemGenericListClass = nullptr;
  421. mSystemGenericDictionaryClass = nullptr;
  422. mSystemTypeClass = nullptr;
  423. mSerializeObjectAttribute = nullptr;
  424. mDontSerializeFieldAttribute = nullptr;
  425. mComponentClass = nullptr;
  426. mSceneObjectClass = nullptr;
  427. mMissingComponentClass = nullptr;
  428. mSerializeFieldAttribute = nullptr;
  429. mHideInInspectorAttribute = nullptr;
  430. }
  431. void ScriptAssemblyManager::initializeBaseTypes()
  432. {
  433. // Get necessary classes for detecting needed class & field information
  434. MonoAssembly* corlib = MonoManager::instance().getAssembly("corlib");
  435. if(corlib == nullptr)
  436. BS_EXCEPT(InvalidStateException, "corlib assembly is not loaded.");
  437. MonoAssembly* bansheeEngineAssembly = MonoManager::instance().getAssembly(ENGINE_ASSEMBLY);
  438. if(bansheeEngineAssembly == nullptr)
  439. BS_EXCEPT(InvalidStateException, String(ENGINE_ASSEMBLY) + " assembly is not loaded.");
  440. mSystemArrayClass = corlib->getClass("System", "Array");
  441. if(mSystemArrayClass == nullptr)
  442. BS_EXCEPT(InvalidStateException, "Cannot find System.Array managed class.");
  443. mSystemGenericListClass = corlib->getClass("System.Collections.Generic", "List`1");
  444. if(mSystemGenericListClass == nullptr)
  445. BS_EXCEPT(InvalidStateException, "Cannot find List<T> managed class.");
  446. mSystemGenericDictionaryClass = corlib->getClass("System.Collections.Generic", "Dictionary`2");
  447. if(mSystemGenericDictionaryClass == nullptr)
  448. BS_EXCEPT(InvalidStateException, "Cannot find Dictionary<TKey, TValue> managed class.");
  449. mSystemTypeClass = corlib->getClass("System", "Type");
  450. if (mSystemTypeClass == nullptr)
  451. BS_EXCEPT(InvalidStateException, "Cannot find Type managed class.");
  452. mSerializeObjectAttribute = bansheeEngineAssembly->getClass("BansheeEngine", "SerializeObject");
  453. if(mSerializeObjectAttribute == nullptr)
  454. BS_EXCEPT(InvalidStateException, "Cannot find SerializableObject managed class.");
  455. mDontSerializeFieldAttribute = bansheeEngineAssembly->getClass("BansheeEngine", "DontSerializeField");
  456. if(mDontSerializeFieldAttribute == nullptr)
  457. BS_EXCEPT(InvalidStateException, "Cannot find DontSerializeField managed class.");
  458. mComponentClass = bansheeEngineAssembly->getClass("BansheeEngine", "Component");
  459. if(mComponentClass == nullptr)
  460. BS_EXCEPT(InvalidStateException, "Cannot find Component managed class.");
  461. mMissingComponentClass = bansheeEngineAssembly->getClass("BansheeEngine", "MissingComponent");
  462. if (mMissingComponentClass == nullptr)
  463. BS_EXCEPT(InvalidStateException, "Cannot find MissingComponent managed class.");
  464. mSceneObjectClass = bansheeEngineAssembly->getClass("BansheeEngine", "SceneObject");
  465. if(mSceneObjectClass == nullptr)
  466. BS_EXCEPT(InvalidStateException, "Cannot find SceneObject managed class.");
  467. mSerializeFieldAttribute = bansheeEngineAssembly->getClass("BansheeEngine", "SerializeField");
  468. if(mSerializeFieldAttribute == nullptr)
  469. BS_EXCEPT(InvalidStateException, "Cannot find SerializeField managed class.");
  470. mHideInInspectorAttribute = bansheeEngineAssembly->getClass("BansheeEngine", "HideInInspector");
  471. if(mHideInInspectorAttribute == nullptr)
  472. BS_EXCEPT(InvalidStateException, "Cannot find HideInInspector managed class.");
  473. mBaseTypesInitialized = true;
  474. }
  475. bool ScriptAssemblyManager::getSerializableObjectInfo(const String& ns, const String& typeName, std::shared_ptr<ManagedSerializableObjectInfo>& outInfo)
  476. {
  477. String fullName = ns + "." + typeName;
  478. for(auto& curAssembly : mAssemblyInfos)
  479. {
  480. if (curAssembly.second == nullptr)
  481. continue;
  482. auto iterFind = curAssembly.second->mTypeNameToId.find(fullName);
  483. if(iterFind != curAssembly.second->mTypeNameToId.end())
  484. {
  485. outInfo = curAssembly.second->mObjectInfos[iterFind->second];
  486. return true;
  487. }
  488. }
  489. return false;
  490. }
  491. bool ScriptAssemblyManager::hasSerializableObjectInfo(const String& ns, const String& typeName)
  492. {
  493. String fullName = ns + "." + typeName;
  494. for(auto& curAssembly : mAssemblyInfos)
  495. {
  496. auto iterFind = curAssembly.second->mTypeNameToId.find(fullName);
  497. if(iterFind != curAssembly.second->mTypeNameToId.end())
  498. return true;
  499. }
  500. return false;
  501. }
  502. }