2
0

BsScriptAssemblyManager.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. #include "BsScriptAssemblyManager.h"
  2. #include "BsScriptResourceManager.h"
  3. #include "BsScriptGameObjectManager.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. #include "BsMonoUtil.h"
  27. #include "BsRTTIType.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(mSceneObjectClass))
  328. {
  329. std::shared_ptr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  330. typeInfo->mType = ScriptPrimitiveType::SceneObjectRef;
  331. return typeInfo;
  332. }
  333. else if(monoClass->isSubClassOf(mComponentClass))
  334. {
  335. std::shared_ptr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  336. typeInfo->mType = ScriptPrimitiveType::ComponentRef;
  337. return typeInfo;
  338. }
  339. else
  340. {
  341. std::shared_ptr<ManagedSerializableObjectInfo> objInfo;
  342. if (getSerializableObjectInfo(monoClass->getNamespace(), monoClass->getTypeName(), objInfo))
  343. return objInfo->mTypeInfo;
  344. }
  345. break;
  346. case MONO_TYPE_VALUETYPE:
  347. {
  348. std::shared_ptr<ManagedSerializableObjectInfo> objInfo;
  349. if (getSerializableObjectInfo(monoClass->getNamespace(), monoClass->getTypeName(), objInfo))
  350. return objInfo->mTypeInfo;
  351. }
  352. break;
  353. case MONO_TYPE_GENERICINST:
  354. if(monoClass->getFullName() == mSystemGenericListClass->getFullName()) // Full name is part of CIL spec, so it is just fine to compare like this
  355. {
  356. std::shared_ptr<ManagedSerializableTypeInfoList> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoList>();
  357. MonoProperty& itemProperty = monoClass->getProperty("Item");
  358. MonoClass* itemClass = itemProperty.getReturnType();
  359. if(itemClass != nullptr)
  360. typeInfo->mElementType = getTypeInfo(itemClass);
  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. 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. typeInfo->mRank = (UINT32)mono_class_get_rank(monoClass->_getInternalClass());
  393. return typeInfo;
  394. }
  395. }
  396. return nullptr;
  397. }
  398. void ScriptAssemblyManager::clearScriptObjects()
  399. {
  400. mBaseTypesInitialized = false;
  401. mSystemArrayClass = nullptr;
  402. mSystemGenericListClass = nullptr;
  403. mSystemGenericDictionaryClass = nullptr;
  404. mSystemTypeClass = nullptr;
  405. mSerializeObjectAttribute = nullptr;
  406. mDontSerializeFieldAttribute = nullptr;
  407. mComponentClass = nullptr;
  408. mSceneObjectClass = nullptr;
  409. mMissingComponentClass = nullptr;
  410. mSerializeFieldAttribute = nullptr;
  411. mHideInInspectorAttribute = nullptr;
  412. }
  413. void ScriptAssemblyManager::initializeBaseTypes()
  414. {
  415. // Get necessary classes for detecting needed class & field information
  416. MonoAssembly* corlib = MonoManager::instance().getAssembly("corlib");
  417. if(corlib == nullptr)
  418. BS_EXCEPT(InvalidStateException, "corlib assembly is not loaded.");
  419. MonoAssembly* bansheeEngineAssembly = MonoManager::instance().getAssembly(ENGINE_ASSEMBLY);
  420. if(bansheeEngineAssembly == nullptr)
  421. BS_EXCEPT(InvalidStateException, String(ENGINE_ASSEMBLY) + " assembly is not loaded.");
  422. mSystemArrayClass = corlib->getClass("System", "Array");
  423. if(mSystemArrayClass == nullptr)
  424. BS_EXCEPT(InvalidStateException, "Cannot find System.Array managed class.");
  425. mSystemGenericListClass = corlib->getClass("System.Collections.Generic", "List`1");
  426. if(mSystemGenericListClass == nullptr)
  427. BS_EXCEPT(InvalidStateException, "Cannot find List<T> managed class.");
  428. mSystemGenericDictionaryClass = corlib->getClass("System.Collections.Generic", "Dictionary`2");
  429. if(mSystemGenericDictionaryClass == nullptr)
  430. BS_EXCEPT(InvalidStateException, "Cannot find Dictionary<TKey, TValue> managed class.");
  431. mSystemTypeClass = corlib->getClass("System", "Type");
  432. if (mSystemTypeClass == nullptr)
  433. BS_EXCEPT(InvalidStateException, "Cannot find Type managed class.");
  434. mSerializeObjectAttribute = bansheeEngineAssembly->getClass("BansheeEngine", "SerializeObject");
  435. if(mSerializeObjectAttribute == nullptr)
  436. BS_EXCEPT(InvalidStateException, "Cannot find SerializableObject managed class.");
  437. mDontSerializeFieldAttribute = bansheeEngineAssembly->getClass("BansheeEngine", "DontSerializeField");
  438. if(mDontSerializeFieldAttribute == nullptr)
  439. BS_EXCEPT(InvalidStateException, "Cannot find DontSerializeField managed class.");
  440. mComponentClass = bansheeEngineAssembly->getClass("BansheeEngine", "Component");
  441. if(mComponentClass == nullptr)
  442. BS_EXCEPT(InvalidStateException, "Cannot find Component managed class.");
  443. mMissingComponentClass = bansheeEngineAssembly->getClass("BansheeEngine", "MissingComponent");
  444. if (mMissingComponentClass == nullptr)
  445. BS_EXCEPT(InvalidStateException, "Cannot find MissingComponent managed class.");
  446. mSceneObjectClass = bansheeEngineAssembly->getClass("BansheeEngine", "SceneObject");
  447. if(mSceneObjectClass == nullptr)
  448. BS_EXCEPT(InvalidStateException, "Cannot find SceneObject managed class.");
  449. mSerializeFieldAttribute = bansheeEngineAssembly->getClass("BansheeEngine", "SerializeField");
  450. if(mSerializeFieldAttribute == nullptr)
  451. BS_EXCEPT(InvalidStateException, "Cannot find SerializeField managed class.");
  452. mHideInInspectorAttribute = bansheeEngineAssembly->getClass("BansheeEngine", "HideInInspector");
  453. if(mHideInInspectorAttribute == nullptr)
  454. BS_EXCEPT(InvalidStateException, "Cannot find HideInInspector managed class.");
  455. mBaseTypesInitialized = true;
  456. }
  457. bool ScriptAssemblyManager::getSerializableObjectInfo(const String& ns, const String& typeName, std::shared_ptr<ManagedSerializableObjectInfo>& outInfo)
  458. {
  459. String fullName = ns + "." + typeName;
  460. for(auto& curAssembly : mAssemblyInfos)
  461. {
  462. if (curAssembly.second == nullptr)
  463. continue;
  464. auto iterFind = curAssembly.second->mTypeNameToId.find(fullName);
  465. if(iterFind != curAssembly.second->mTypeNameToId.end())
  466. {
  467. outInfo = curAssembly.second->mObjectInfos[iterFind->second];
  468. return true;
  469. }
  470. }
  471. return false;
  472. }
  473. bool ScriptAssemblyManager::hasSerializableObjectInfo(const String& ns, const String& typeName)
  474. {
  475. String fullName = ns + "." + typeName;
  476. for(auto& curAssembly : mAssemblyInfos)
  477. {
  478. auto iterFind = curAssembly.second->mTypeNameToId.find(fullName);
  479. if(iterFind != curAssembly.second->mTypeNameToId.end())
  480. return true;
  481. }
  482. return false;
  483. }
  484. }