BsScriptAssemblyManager.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  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 "BsScriptComponent.h"
  13. #include "BsScriptTexture2D.h"
  14. #include "BsScriptTexture3D.h"
  15. #include "BsScriptTextureCube.h"
  16. #include "BsScriptSpriteTexture.h"
  17. #include "BsScriptMaterial.h"
  18. #include "BsScriptMesh.h"
  19. #include "BsScriptFont.h"
  20. #include "BsScriptShader.h"
  21. #include "BsScriptShaderInclude.h"
  22. #include "BsScriptPlainText.h"
  23. #include "BsScriptScriptCode.h"
  24. #include "BsScriptStringTable.h"
  25. #include "BsScriptGUISkin.h"
  26. #include "BsScriptPhysicsMaterial.h"
  27. #include "BsScriptPhysicsMesh.h"
  28. #include "BsScriptAudioClip.h"
  29. #include "BsScriptPrefab.h"
  30. #include "BsScriptAnimationClip.h"
  31. #include "BsBuiltinComponentLookup.h"
  32. namespace bs
  33. {
  34. ScriptAssemblyManager::ScriptAssemblyManager()
  35. : mBaseTypesInitialized(false), mSystemArrayClass(nullptr), mSystemGenericListClass(nullptr)
  36. , mSystemGenericDictionaryClass(nullptr), mSystemTypeClass(nullptr), mComponentClass(nullptr)
  37. , mManagedComponentClass(nullptr), mSceneObjectClass(nullptr), mMissingComponentClass(nullptr)
  38. , mSerializeObjectAttribute(nullptr), mDontSerializeFieldAttribute(nullptr), mSerializeFieldAttribute(nullptr)
  39. , mHideInInspectorAttribute(nullptr), mShowInInspectorAttribute(nullptr), mRangeAttribute(nullptr)
  40. , mStepAttribute(nullptr)
  41. {
  42. }
  43. ScriptAssemblyManager::~ScriptAssemblyManager()
  44. {
  45. }
  46. Vector<String> ScriptAssemblyManager::getScriptAssemblies() const
  47. {
  48. Vector<String> initializedAssemblies;
  49. for (auto& assemblyPair : mAssemblyInfos)
  50. initializedAssemblies.push_back(assemblyPair.first);
  51. return initializedAssemblies;
  52. }
  53. void ScriptAssemblyManager::loadAssemblyInfo(const String& assemblyName)
  54. {
  55. if(!mBaseTypesInitialized)
  56. initializeBaseTypes();
  57. initializeBuiltinComponentInfos();
  58. // Process all classes and fields
  59. UINT32 mUniqueTypeId = 1;
  60. MonoAssembly* curAssembly = MonoManager::instance().getAssembly(assemblyName);
  61. if(curAssembly == nullptr)
  62. return;
  63. SPtr<ManagedSerializableAssemblyInfo> assemblyInfo = bs_shared_ptr_new<ManagedSerializableAssemblyInfo>();
  64. assemblyInfo->mName = assemblyName;
  65. mAssemblyInfos[assemblyName] = assemblyInfo;
  66. MonoClass* managedResourceClass = ScriptManagedResource::getMetaData()->scriptClass;
  67. // Populate class data
  68. const Vector<MonoClass*>& allClasses = curAssembly->getAllClasses();
  69. for(auto& curClass : allClasses)
  70. {
  71. if ((curClass->isSubClassOf(mManagedComponentClass) || curClass->isSubClassOf(managedResourceClass) ||
  72. curClass->hasAttribute(mSerializeObjectAttribute)) && curClass != mManagedComponentClass &&
  73. curClass != managedResourceClass)
  74. {
  75. SPtr<ManagedSerializableTypeInfoObject> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoObject>();
  76. typeInfo->mTypeNamespace = curClass->getNamespace();
  77. typeInfo->mTypeName = curClass->getTypeName();
  78. typeInfo->mTypeId = mUniqueTypeId++;
  79. MonoPrimitiveType monoPrimitiveType = MonoUtil::getPrimitiveType(curClass->_getInternalClass());
  80. if(monoPrimitiveType == MonoPrimitiveType::ValueType)
  81. typeInfo->mValueType = true;
  82. else
  83. typeInfo->mValueType = false;
  84. SPtr<ManagedSerializableObjectInfo> objInfo = bs_shared_ptr_new<ManagedSerializableObjectInfo>();
  85. objInfo->mTypeInfo = typeInfo;
  86. objInfo->mMonoClass = curClass;
  87. assemblyInfo->mTypeNameToId[objInfo->getFullTypeName()] = typeInfo->mTypeId;
  88. assemblyInfo->mObjectInfos[typeInfo->mTypeId] = objInfo;
  89. }
  90. }
  91. // Populate field & property data
  92. for(auto& curClassInfo : assemblyInfo->mObjectInfos)
  93. {
  94. SPtr<ManagedSerializableObjectInfo> objInfo = curClassInfo.second;
  95. UINT32 mUniqueFieldId = 1;
  96. const Vector<MonoField*>& fields = objInfo->mMonoClass->getAllFields();
  97. for(auto& field : fields)
  98. {
  99. if(field->isStatic())
  100. continue;
  101. SPtr<ManagedSerializableTypeInfo> typeInfo = getTypeInfo(field->getType());
  102. if (typeInfo == nullptr)
  103. continue;
  104. SPtr<ManagedSerializableFieldInfo> fieldInfo = bs_shared_ptr_new<ManagedSerializableFieldInfo>();
  105. fieldInfo->mFieldId = mUniqueFieldId++;
  106. fieldInfo->mName = field->getName();
  107. fieldInfo->mMonoField = field;
  108. fieldInfo->mTypeInfo = typeInfo;
  109. fieldInfo->mParentTypeId = objInfo->mTypeInfo->mTypeId;
  110. MonoMemberVisibility visibility = field->getVisibility();
  111. if (visibility == MonoMemberVisibility::Public)
  112. {
  113. if (!field->hasAttribute(mDontSerializeFieldAttribute))
  114. fieldInfo->mFlags |= ScriptFieldFlag::Serializable;
  115. if (!field->hasAttribute(mHideInInspectorAttribute))
  116. fieldInfo->mFlags |= ScriptFieldFlag::Inspectable;
  117. fieldInfo->mFlags |= ScriptFieldFlag::Animable;
  118. }
  119. else
  120. {
  121. if (field->hasAttribute(mSerializeFieldAttribute))
  122. fieldInfo->mFlags |= ScriptFieldFlag::Serializable;
  123. if (field->hasAttribute(mShowInInspectorAttribute))
  124. fieldInfo->mFlags |= ScriptFieldFlag::Inspectable;
  125. }
  126. if (field->hasAttribute(mRangeAttribute))
  127. fieldInfo->mFlags |= ScriptFieldFlag::Range;
  128. if (field->hasAttribute(mStepAttribute))
  129. fieldInfo->mFlags |= ScriptFieldFlag::Step;
  130. objInfo->mFieldNameToId[fieldInfo->mName] = fieldInfo->mFieldId;
  131. objInfo->mFields[fieldInfo->mFieldId] = fieldInfo;
  132. }
  133. const Vector<MonoProperty*>& properties = objInfo->mMonoClass->getAllProperties();
  134. for (auto& property : properties)
  135. {
  136. SPtr<ManagedSerializableTypeInfo> typeInfo = getTypeInfo(property->getReturnType());
  137. if (typeInfo == nullptr)
  138. continue;
  139. SPtr<ManagedSerializablePropertyInfo> propertyInfo = bs_shared_ptr_new<ManagedSerializablePropertyInfo>();
  140. propertyInfo->mFieldId = mUniqueFieldId++;
  141. propertyInfo->mName = property->getName();
  142. propertyInfo->mMonoProperty = property;
  143. propertyInfo->mTypeInfo = typeInfo;
  144. propertyInfo->mParentTypeId = objInfo->mTypeInfo->mTypeId;
  145. if (!property->isIndexed())
  146. {
  147. MonoMemberVisibility visibility = property->getVisibility();
  148. if (visibility == MonoMemberVisibility::Public)
  149. propertyInfo->mFlags |= ScriptFieldFlag::Animable;
  150. if (property->hasAttribute(mSerializeFieldAttribute))
  151. propertyInfo->mFlags |= ScriptFieldFlag::Serializable;
  152. if (property->hasAttribute(mShowInInspectorAttribute))
  153. propertyInfo->mFlags |= ScriptFieldFlag::Inspectable;
  154. }
  155. if (property->hasAttribute(mRangeAttribute))
  156. propertyInfo->mFlags |= ScriptFieldFlag::Range;
  157. if (property->hasAttribute(mStepAttribute))
  158. propertyInfo->mFlags |= ScriptFieldFlag::Step;
  159. objInfo->mFieldNameToId[propertyInfo->mName] = propertyInfo->mFieldId;
  160. objInfo->mFields[propertyInfo->mFieldId] = propertyInfo;
  161. }
  162. }
  163. // Form parent/child connections
  164. for(auto& curClass : assemblyInfo->mObjectInfos)
  165. {
  166. MonoClass* base = curClass.second->mMonoClass->getBaseClass();
  167. while(base != nullptr)
  168. {
  169. SPtr<ManagedSerializableObjectInfo> baseObjInfo;
  170. if(getSerializableObjectInfo(base->getNamespace(), base->getTypeName(), baseObjInfo))
  171. {
  172. curClass.second->mBaseClass = baseObjInfo;
  173. baseObjInfo->mDerivedClasses.push_back(curClass.second);
  174. break;
  175. }
  176. base = base->getBaseClass();
  177. }
  178. }
  179. }
  180. void ScriptAssemblyManager::clearAssemblyInfo()
  181. {
  182. clearScriptObjects();
  183. mAssemblyInfos.clear();
  184. }
  185. SPtr<ManagedSerializableTypeInfo> ScriptAssemblyManager::getTypeInfo(MonoClass* monoClass)
  186. {
  187. if(!mBaseTypesInitialized)
  188. BS_EXCEPT(InvalidStateException, "Calling getTypeInfo without previously initializing base types.");
  189. MonoPrimitiveType monoPrimitiveType = MonoUtil::getPrimitiveType(monoClass->_getInternalClass());
  190. // If enum get the enum base data type
  191. bool isEnum = MonoUtil::isEnum(monoClass->_getInternalClass());
  192. if (isEnum)
  193. monoPrimitiveType = MonoUtil::getEnumPrimitiveType(monoClass->_getInternalClass());
  194. // Determine field type
  195. switch(monoPrimitiveType)
  196. {
  197. case MonoPrimitiveType::Boolean:
  198. {
  199. SPtr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  200. typeInfo->mType = ScriptPrimitiveType::Bool;
  201. return typeInfo;
  202. }
  203. case MonoPrimitiveType::Char:
  204. {
  205. SPtr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  206. typeInfo->mType = ScriptPrimitiveType::Char;
  207. return typeInfo;
  208. }
  209. case MonoPrimitiveType::I8:
  210. {
  211. SPtr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  212. typeInfo->mType = ScriptPrimitiveType::I8;
  213. return typeInfo;
  214. }
  215. case MonoPrimitiveType::U8:
  216. {
  217. SPtr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  218. typeInfo->mType = ScriptPrimitiveType::U8;
  219. return typeInfo;
  220. }
  221. case MonoPrimitiveType::I16:
  222. {
  223. SPtr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  224. typeInfo->mType = ScriptPrimitiveType::I16;
  225. return typeInfo;
  226. }
  227. case MonoPrimitiveType::U16:
  228. {
  229. SPtr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  230. typeInfo->mType = ScriptPrimitiveType::U16;
  231. return typeInfo;
  232. }
  233. case MonoPrimitiveType::I32:
  234. {
  235. SPtr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  236. typeInfo->mType = ScriptPrimitiveType::I32;
  237. return typeInfo;
  238. }
  239. case MonoPrimitiveType::U32:
  240. {
  241. SPtr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  242. typeInfo->mType = ScriptPrimitiveType::U32;
  243. return typeInfo;
  244. }
  245. case MonoPrimitiveType::I64:
  246. {
  247. SPtr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  248. typeInfo->mType = ScriptPrimitiveType::I64;
  249. return typeInfo;
  250. }
  251. case MonoPrimitiveType::U64:
  252. {
  253. SPtr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  254. typeInfo->mType = ScriptPrimitiveType::U64;
  255. return typeInfo;
  256. }
  257. case MonoPrimitiveType::String:
  258. {
  259. SPtr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  260. typeInfo->mType = ScriptPrimitiveType::String;
  261. return typeInfo;
  262. }
  263. case MonoPrimitiveType::R32:
  264. {
  265. SPtr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  266. typeInfo->mType = ScriptPrimitiveType::Float;
  267. return typeInfo;
  268. }
  269. case MonoPrimitiveType::R64:
  270. {
  271. SPtr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  272. typeInfo->mType = ScriptPrimitiveType::Double;
  273. return typeInfo;
  274. }
  275. case MonoPrimitiveType::Class:
  276. if(monoClass->isSubClassOf(ScriptResource::getMetaData()->scriptClass)) // Resource
  277. {
  278. SPtr<ManagedSerializableTypeInfoRef> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoRef>();
  279. typeInfo->mTypeNamespace = monoClass->getNamespace();
  280. typeInfo->mTypeName = monoClass->getTypeName();
  281. if(monoClass == ScriptResource::getMetaData()->scriptClass)
  282. typeInfo->mType = ScriptReferenceType::Resource;
  283. else if (monoClass->isSubClassOf(ScriptTexture2D::getMetaData()->scriptClass))
  284. typeInfo->mType = ScriptReferenceType::Texture2D;
  285. else if (monoClass->isSubClassOf(ScriptTexture3D::getMetaData()->scriptClass))
  286. typeInfo->mType = ScriptReferenceType::Texture3D;
  287. else if (monoClass->isSubClassOf(ScriptTextureCube::getMetaData()->scriptClass))
  288. typeInfo->mType = ScriptReferenceType::TextureCube;
  289. else if (monoClass->isSubClassOf(ScriptSpriteTexture::getMetaData()->scriptClass))
  290. typeInfo->mType = ScriptReferenceType::SpriteTexture;
  291. else if (monoClass->isSubClassOf(ScriptManagedResource::getMetaData()->scriptClass))
  292. typeInfo->mType = ScriptReferenceType::ManagedResource;
  293. else if (monoClass->isSubClassOf(ScriptShader::getMetaData()->scriptClass))
  294. typeInfo->mType = ScriptReferenceType::Shader;
  295. else if (monoClass->isSubClassOf(ScriptShaderInclude::getMetaData()->scriptClass))
  296. typeInfo->mType = ScriptReferenceType::ShaderInclude;
  297. else if (monoClass->isSubClassOf(ScriptMaterial::getMetaData()->scriptClass))
  298. typeInfo->mType = ScriptReferenceType::Material;
  299. else if (monoClass->isSubClassOf(ScriptMesh::getMetaData()->scriptClass))
  300. typeInfo->mType = ScriptReferenceType::Mesh;
  301. else if (monoClass->isSubClassOf(ScriptPlainText::getMetaData()->scriptClass))
  302. typeInfo->mType = ScriptReferenceType::PlainText;
  303. else if (monoClass->isSubClassOf(ScriptScriptCode::getMetaData()->scriptClass))
  304. typeInfo->mType = ScriptReferenceType::ScriptCode;
  305. else if (monoClass->isSubClassOf(ScriptPrefab::getMetaData()->scriptClass))
  306. typeInfo->mType = ScriptReferenceType::Prefab;
  307. else if (monoClass->isSubClassOf(ScriptFont::getMetaData()->scriptClass))
  308. typeInfo->mType = ScriptReferenceType::Font;
  309. else if (monoClass->isSubClassOf(ScriptStringTable::getMetaData()->scriptClass))
  310. typeInfo->mType = ScriptReferenceType::StringTable;
  311. else if (monoClass->isSubClassOf(ScriptGUISkin::getMetaData()->scriptClass))
  312. typeInfo->mType = ScriptReferenceType::GUISkin;
  313. else if (monoClass->isSubClassOf(ScriptPhysicsMaterial::getMetaData()->scriptClass))
  314. typeInfo->mType = ScriptReferenceType::PhysicsMaterial;
  315. else if (monoClass->isSubClassOf(ScriptPhysicsMesh::getMetaData()->scriptClass))
  316. typeInfo->mType = ScriptReferenceType::PhysicsMesh;
  317. else if (monoClass->isSubClassOf(ScriptAudioClip::getMetaData()->scriptClass))
  318. typeInfo->mType = ScriptReferenceType::AudioClip;
  319. else if (monoClass->isSubClassOf(ScriptAnimationClip::getMetaData()->scriptClass))
  320. typeInfo->mType = ScriptReferenceType::AnimationClip;
  321. else
  322. {
  323. assert(false && "Unrecognized resource type");
  324. }
  325. return typeInfo;
  326. }
  327. else if (monoClass->isSubClassOf(mSceneObjectClass) || monoClass->isSubClassOf(mComponentClass)) // Game object
  328. {
  329. SPtr<ManagedSerializableTypeInfoRef> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoRef>();
  330. typeInfo->mTypeNamespace = monoClass->getNamespace();
  331. typeInfo->mTypeName = monoClass->getTypeName();
  332. typeInfo->mRTIITypeId = 0;
  333. if (monoClass == mComponentClass)
  334. typeInfo->mType = ScriptReferenceType::BuiltinComponentBase;
  335. else if (monoClass == mManagedComponentClass)
  336. typeInfo->mType = ScriptReferenceType::ManagedComponentBase;
  337. else if (monoClass->isSubClassOf(mSceneObjectClass))
  338. typeInfo->mType = ScriptReferenceType::SceneObject;
  339. else if (monoClass->isSubClassOf(mManagedComponentClass))
  340. typeInfo->mType = ScriptReferenceType::ManagedComponent;
  341. else if (monoClass->isSubClassOf(mComponentClass))
  342. {
  343. typeInfo->mType = ScriptReferenceType::BuiltinComponent;
  344. ::MonoReflectionType* type = MonoUtil::getType(monoClass->_getInternalClass());
  345. BuiltinComponentInfo* builtinInfo = getBuiltinComponentInfo(type);
  346. if(builtinInfo == nullptr)
  347. {
  348. assert(false && "Unable to find information about a built-in component. Did you update BuiltinComponents list?");
  349. return nullptr;
  350. }
  351. typeInfo->mRTIITypeId = builtinInfo->typeId;
  352. }
  353. return typeInfo;
  354. }
  355. else
  356. {
  357. SPtr<ManagedSerializableObjectInfo> objInfo;
  358. if (getSerializableObjectInfo(monoClass->getNamespace(), monoClass->getTypeName(), objInfo))
  359. return objInfo->mTypeInfo;
  360. }
  361. break;
  362. case MonoPrimitiveType::ValueType:
  363. {
  364. SPtr<ManagedSerializableObjectInfo> objInfo;
  365. if (getSerializableObjectInfo(monoClass->getNamespace(), monoClass->getTypeName(), objInfo))
  366. return objInfo->mTypeInfo;
  367. }
  368. break;
  369. case MonoPrimitiveType::Generic:
  370. if(monoClass->getFullName() == mSystemGenericListClass->getFullName()) // Full name is part of CIL spec, so it is just fine to compare like this
  371. {
  372. SPtr<ManagedSerializableTypeInfoList> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoList>();
  373. MonoProperty* itemProperty = monoClass->getProperty("Item");
  374. MonoClass* itemClass = itemProperty->getReturnType();
  375. if (itemClass != nullptr)
  376. typeInfo->mElementType = getTypeInfo(itemClass);
  377. if (typeInfo->mElementType == nullptr)
  378. return nullptr;
  379. return typeInfo;
  380. }
  381. else if(monoClass->getFullName() == mSystemGenericDictionaryClass->getFullName())
  382. {
  383. SPtr<ManagedSerializableTypeInfoDictionary> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoDictionary>();
  384. MonoMethod* getEnumerator = monoClass->getMethod("GetEnumerator");
  385. MonoClass* enumClass = getEnumerator->getReturnType();
  386. MonoProperty* currentProp = enumClass->getProperty("Current");
  387. MonoClass* keyValuePair = currentProp->getReturnType();
  388. MonoProperty* keyProperty = keyValuePair->getProperty("Key");
  389. MonoProperty* valueProperty = keyValuePair->getProperty("Value");
  390. MonoClass* keyClass = keyProperty->getReturnType();
  391. if(keyClass != nullptr)
  392. typeInfo->mKeyType = getTypeInfo(keyClass);
  393. MonoClass* valueClass = valueProperty->getReturnType();
  394. if(valueClass != nullptr)
  395. typeInfo->mValueType = getTypeInfo(valueClass);
  396. if (typeInfo->mKeyType == nullptr || typeInfo->mValueType == nullptr)
  397. return nullptr;
  398. return typeInfo;
  399. }
  400. break;
  401. case MonoPrimitiveType::Array:
  402. {
  403. SPtr<ManagedSerializableTypeInfoArray> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoArray>();
  404. ::MonoClass* elementClass = ScriptArray::getElementClass(monoClass->_getInternalClass());
  405. if(elementClass != nullptr)
  406. {
  407. MonoClass* monoElementClass = MonoManager::instance().findClass(elementClass);
  408. if(monoElementClass != nullptr)
  409. typeInfo->mElementType = getTypeInfo(monoElementClass);
  410. }
  411. if (typeInfo->mElementType == nullptr)
  412. return nullptr;
  413. typeInfo->mRank = ScriptArray::getRank(monoClass->_getInternalClass());
  414. return typeInfo;
  415. }
  416. default:
  417. break;
  418. }
  419. return nullptr;
  420. }
  421. void ScriptAssemblyManager::clearScriptObjects()
  422. {
  423. mBaseTypesInitialized = false;
  424. mSystemArrayClass = nullptr;
  425. mSystemGenericListClass = nullptr;
  426. mSystemGenericDictionaryClass = nullptr;
  427. mSystemTypeClass = nullptr;
  428. mSerializeObjectAttribute = nullptr;
  429. mDontSerializeFieldAttribute = nullptr;
  430. mComponentClass = nullptr;
  431. mManagedComponentClass = nullptr;
  432. mSceneObjectClass = nullptr;
  433. mMissingComponentClass = nullptr;
  434. mSerializeFieldAttribute = nullptr;
  435. mHideInInspectorAttribute = nullptr;
  436. mShowInInspectorAttribute = nullptr;
  437. mRangeAttribute = nullptr;
  438. mStepAttribute = nullptr;
  439. }
  440. void ScriptAssemblyManager::initializeBaseTypes()
  441. {
  442. // Get necessary classes for detecting needed class & field information
  443. MonoAssembly* corlib = MonoManager::instance().getAssembly("corlib");
  444. if(corlib == nullptr)
  445. BS_EXCEPT(InvalidStateException, "corlib assembly is not loaded.");
  446. MonoAssembly* bansheeEngineAssembly = MonoManager::instance().getAssembly(ENGINE_ASSEMBLY);
  447. if(bansheeEngineAssembly == nullptr)
  448. BS_EXCEPT(InvalidStateException, String(ENGINE_ASSEMBLY) + " assembly is not loaded.");
  449. mSystemArrayClass = corlib->getClass("System", "Array");
  450. if(mSystemArrayClass == nullptr)
  451. BS_EXCEPT(InvalidStateException, "Cannot find System.Array managed class.");
  452. mSystemGenericListClass = corlib->getClass("System.Collections.Generic", "List`1");
  453. if(mSystemGenericListClass == nullptr)
  454. BS_EXCEPT(InvalidStateException, "Cannot find List<T> managed class.");
  455. mSystemGenericDictionaryClass = corlib->getClass("System.Collections.Generic", "Dictionary`2");
  456. if(mSystemGenericDictionaryClass == nullptr)
  457. BS_EXCEPT(InvalidStateException, "Cannot find Dictionary<TKey, TValue> managed class.");
  458. mSystemTypeClass = corlib->getClass("System", "Type");
  459. if (mSystemTypeClass == nullptr)
  460. BS_EXCEPT(InvalidStateException, "Cannot find Type managed class.");
  461. mSerializeObjectAttribute = bansheeEngineAssembly->getClass("BansheeEngine", "SerializeObject");
  462. if(mSerializeObjectAttribute == nullptr)
  463. BS_EXCEPT(InvalidStateException, "Cannot find SerializableObject managed class.");
  464. mDontSerializeFieldAttribute = bansheeEngineAssembly->getClass("BansheeEngine", "DontSerializeField");
  465. if(mDontSerializeFieldAttribute == nullptr)
  466. BS_EXCEPT(InvalidStateException, "Cannot find DontSerializeField managed class.");
  467. mRangeAttribute = bansheeEngineAssembly->getClass("BansheeEngine", "Range");
  468. if (mRangeAttribute == nullptr)
  469. BS_EXCEPT(InvalidStateException, "Cannot find Range managed class.");
  470. mStepAttribute = bansheeEngineAssembly->getClass("BansheeEngine", "Step");
  471. if (mStepAttribute == nullptr)
  472. BS_EXCEPT(InvalidStateException, "Cannot find Step managed class.");
  473. mComponentClass = bansheeEngineAssembly->getClass("BansheeEngine", "Component");
  474. if(mComponentClass == nullptr)
  475. BS_EXCEPT(InvalidStateException, "Cannot find Component managed class.");
  476. mManagedComponentClass = bansheeEngineAssembly->getClass("BansheeEngine", "ManagedComponent");
  477. if (mManagedComponentClass == nullptr)
  478. BS_EXCEPT(InvalidStateException, "Cannot find ManagedComponent managed class.");
  479. mMissingComponentClass = bansheeEngineAssembly->getClass("BansheeEngine", "MissingComponent");
  480. if (mMissingComponentClass == nullptr)
  481. BS_EXCEPT(InvalidStateException, "Cannot find MissingComponent managed class.");
  482. mSceneObjectClass = bansheeEngineAssembly->getClass("BansheeEngine", "SceneObject");
  483. if(mSceneObjectClass == nullptr)
  484. BS_EXCEPT(InvalidStateException, "Cannot find SceneObject managed class.");
  485. mSerializeFieldAttribute = bansheeEngineAssembly->getClass("BansheeEngine", "SerializeField");
  486. if(mSerializeFieldAttribute == nullptr)
  487. BS_EXCEPT(InvalidStateException, "Cannot find SerializeField managed class.");
  488. mHideInInspectorAttribute = bansheeEngineAssembly->getClass("BansheeEngine", "HideInInspector");
  489. if(mHideInInspectorAttribute == nullptr)
  490. BS_EXCEPT(InvalidStateException, "Cannot find HideInInspector managed class.");
  491. mShowInInspectorAttribute = bansheeEngineAssembly->getClass("BansheeEngine", "ShowInInspector");
  492. if (mShowInInspectorAttribute == nullptr)
  493. BS_EXCEPT(InvalidStateException, "Cannot find ShowInInspector managed class.");
  494. mBaseTypesInitialized = true;
  495. }
  496. void ScriptAssemblyManager::initializeBuiltinComponentInfos()
  497. {
  498. mBuiltinComponentInfos.clear();
  499. mBuiltinComponentInfosByTID.clear();
  500. Vector<BuiltinComponentInfo> allComponentsInfos = BuiltinComponents::getEntries();
  501. for(auto& entry : allComponentsInfos)
  502. {
  503. MonoAssembly* assembly = MonoManager::instance().getAssembly(entry.metaData->assembly);
  504. if (assembly == nullptr)
  505. continue;
  506. BuiltinComponentInfo info = entry;
  507. info.monoClass = assembly->getClass(entry.metaData->ns, entry.metaData->name);
  508. ::MonoReflectionType* type = MonoUtil::getType(info.monoClass->_getInternalClass());
  509. mBuiltinComponentInfos[type] = info;
  510. mBuiltinComponentInfosByTID[info.typeId] = info;
  511. }
  512. }
  513. BuiltinComponentInfo* ScriptAssemblyManager::getBuiltinComponentInfo(::MonoReflectionType* type)
  514. {
  515. auto iterFind = mBuiltinComponentInfos.find(type);
  516. if (iterFind == mBuiltinComponentInfos.end())
  517. return nullptr;
  518. return &(iterFind->second);
  519. }
  520. BuiltinComponentInfo* ScriptAssemblyManager::getBuiltinComponentInfo(UINT32 rttiTypeId)
  521. {
  522. auto iterFind = mBuiltinComponentInfosByTID.find(rttiTypeId);
  523. if (iterFind == mBuiltinComponentInfosByTID.end())
  524. return nullptr;
  525. return &(iterFind->second);
  526. }
  527. bool ScriptAssemblyManager::getSerializableObjectInfo(const String& ns, const String& typeName, SPtr<ManagedSerializableObjectInfo>& outInfo)
  528. {
  529. String fullName = ns + "." + typeName;
  530. for(auto& curAssembly : mAssemblyInfos)
  531. {
  532. if (curAssembly.second == nullptr)
  533. continue;
  534. auto iterFind = curAssembly.second->mTypeNameToId.find(fullName);
  535. if(iterFind != curAssembly.second->mTypeNameToId.end())
  536. {
  537. outInfo = curAssembly.second->mObjectInfos[iterFind->second];
  538. return true;
  539. }
  540. }
  541. return false;
  542. }
  543. bool ScriptAssemblyManager::hasSerializableObjectInfo(const String& ns, const String& typeName)
  544. {
  545. String fullName = ns + "." + typeName;
  546. for(auto& curAssembly : mAssemblyInfos)
  547. {
  548. auto iterFind = curAssembly.second->mTypeNameToId.find(fullName);
  549. if(iterFind != curAssembly.second->mTypeNameToId.end())
  550. return true;
  551. }
  552. return false;
  553. }
  554. }