BsScriptAssemblyManager.cpp 22 KB

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