BsScriptAssemblyManager.cpp 21 KB

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